General Webdev and Programming Stuff General Programming Topics Cheatsheet - everything you need to know

General Webdev and Programming Stuff General Programming Topics Cheatsheet

Push vs Pop vs Shift vs Unshift

These four functions - push(), pop(), shift() and unshift - are common in many programming languages. They are used for adding and removing elements to arrays (either from the start or end).

The specific implementations of each function will vary between languages (it might be push($array,'new item'), $array.push('new_item'), etc. But the basic idea is normally the same:

End of array = the last item (if the array has 5 elements, then index #4).
Start = first item, so item #0

Adding to arrays: push() and pop()

push

push() will add (push) an item to the end of an array.

In php, it is array_push(&array, $new_el)

In javascript it is some_array_variable.push(new_el)

unshift

unshift() will add (prepend) an item to the start of an array.

In php, it is array_unshift(&array, $new_el)

In javascript, it is some_array_var.unshift(add_this_obj)

Removing from arrays: pop() and shift()

In most languages these will remove the item from an array, then return that removed item.

pop

pop() will remove (pop) an item from the end of an array.

In php, it is array_pop(&$array)

In javascript, it is some_array.pop()

shift

shift() will remove (shift) an item from the start of an array.

In php, it is array_shift(&array))

In javascript, it is another_array.shift()

Some examples in PHP of adding/removing items from the start and end of arrays:

PHP Push example - Add to the end

  1. $stack = array("orange", "banana", "apple", "raspberry");
  2. array_push($stack, "NEW_ITEM");
  3. print_r(["stack"=>$stack,]);
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
    [4] => NEW_ITEM
)

PHP Unshift example - add to the start

  1. $stack = array("orange", "banana", "apple", "raspberry");
  2. array_unshift($stack, "NEW_ITEM");
  3. print_r($stack);
Array
(
    [0] => NEW_ITEM
    [1] => orange
    [2] => banana
    [3] => apple
    [4] => raspberry
)

PHP Pop example - remove from the end

  1. $stack = array("orange", "banana", "apple", "raspberry");
  2. $removed = array_pop($stack);
  3. print_r(["stack"=>$stack,'removed_item_of_fruit' => $removed]);
Array
(
    [stack] => Array
        (
            [0] => orange
            [1] => banana
            [2] => apple
        )

    [removed_item_of_fruit] => raspberry
)

PHP Shift example - remove from the start

  1. $stack = array("orange", "banana", "apple", "raspberry");
  2. $fruit = array_shift($stack);
  3. print_r($stack);
Array
(
    [stack] => Array
        (
            [0] => banana
            [1] => apple
            [2] => raspberry
        )

    [shifted_item_of_fruit] => orange
)
View More Details (and 12 discussions about this topic) Here...

What is Agile software development?

It is a methodology or approach to software development, which focuses on adaptive planning, evolutionary development, early delivery, and continual improvement, and it encourages rapid and flexible response to change.

View More Details (and 11 discussions about this topic) Here...

What is YAGNI?

YAGNI is a programming principle that says that you should not add functionality until deemed necessary. It stands for 'You aren't gonna need it'.

A related phrase is DTSTTCPW - 'do the simplest thing that could possibly work'.

View More Details (and 10 discussions about this topic) Here...