PHP Functions - Part 1



Thursday, September 6th 2012

I’ve been working with PHP for several years, dare I say a decade at this point, and I’m still surprised at functions I’ve never or rarely used. In an attempt to share in my delight of discovering these gems and hopefully enlighten others to some very helpful functions, I’m going to try to start a series of quick articles explaining various PHP functions. This first part will go over four very inter-related array functions.

Pop, Push, Shift, Unshift

Gotta love the naming conventions sometimes, these make me think of a Daft Punk song. Anyways, these are four array functions that I like to keep lumped together in my mind as they do very similar tasks. Here they are in the order I’m using them in the forthcoming example:

  • array_shift – Takes the first variable off the array and returns it
  • array_pop – Takes the last variable off the array and returns it
  • array_push – Adds a variable onto the end of the array and returns the array count
  • array_unshift – Adds a variable onto the beginning of the array and returns the array count

One thing worth noting that I find helps me remember these functions without having to look them up constantly. The first pair (array_pop and array_shift) and the second pair (array_push and array_unshift) do the same things, the only distinctions are whether it’s targeting the beginning or the end of the array and the returned value. Function array_pop and array_shift return the variable that was removed from the array, array_push and array_unshift return the array count after the addition was made.

Here’s an example that uses all four functions mentioned here to modify an array.


<!--?php <br ?-->$input = array(0, 1, 2, 3, 4, 5);

echo '
<strong>Starting Array</strong>

<code>$input = array (' . implode(', ', $input ) . ')</code>

';

$result = array_shift($input);

echo '
<strong>Using array_shift</strong> - <em>Takes the first variable off the array and returns it</em>

<code>$result = array_shift($input);</code>

<strong>Result:</strong>

<code>$input = array (' . implode(', ', $input ) . ')</code>

<code>$result = ' . $result . '</code>

';

$result = array_pop($input);

echo '
<strong>Using array_pop</strong> - <em>Takes the last variable off the array and returns it</em>

<code>$result = array_pop($input);</code>

<strong>Result:</strong>

<code>$input = array (' . implode(', ', $input ) . ')</code>

<code>$result = ' . $result . '</code>

';

$result = array_push($input, 5);

echo '
<strong>Using array_push</strong> - <em>Adds a variable onto the end of the array and returns the count of the array</em>

<code>$result = array_push($input, 5);</code>

<strong>Result:</strong>

<code>$input = array (' . implode(', ', $input ) . ')</code>

<code>$result = ' . $result . '</code>

';

$result = array_unshift($input, 0);

echo '
<strong>Using array_unshift</strong> - <em>Adds a variable onto the beginning of the array and returns the count of the array</em>

<code>$result = array_unshift($input, 0);</code>

<strong>Result:</strong>

<code>$input = array (' . implode(', ', $input ) . ')</code>

<code>$result = ' . $result . '</code>

';
?>

This code returns:

Starting Array
$input = array (0, 1, 2, 3, 4, 5)

Using array_shiftTakes the first variable off the array and returns it
$result = array_shift($input);
Result:
$input = array (1, 2, 3, 4, 5)
$result = 0

Using array_popTakes the last variable off the array and returns it
$result = array_pop($input);
Result:
$input = array (1, 2, 3, 4)
$result = 5

Using array_pushAdds a variable onto the end of the array and returns the count of the array
$result = array_push($input, 5);
Result:
$input = array (1, 2, 3, 4, 5)
$result = 5

Using array_unshiftAdds a variable onto the beginning of the array and returns the count of the array
$result = array_unshift($input, 0);
Result:
$input = array (0, 1, 2, 3, 4, 5)
$result = 6

A bit cyclical I admit, but it gets the point across. We removed the first then last values from the array and then turned around and added them back in. So we end up with the same array values that we started with.

Stay tuned, in Part 2 I’ll be talking about a few more underutilized array functions.