array_pop

Removes and returns the last element of an array, shortening the array by one.

Signature
array_pop(array &$array): mixed
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe array to pop from. Passed by reference and modified in place.

Return value

mixedThe value of the last element, removed from the array. Returns null if the array is empty.

Errors

No exception is thrown for an empty array — null is returned silently. Passing anything other than a variable raises an error because the parameter is by reference.

array_pop() takes the last element off an array, returns it, and shrinks the array. It is the read-and-remove half of a stack, with array_push() as the other half.

Why it beats array_shift

Popping from the end is an O(1) operation — nothing else has to move. array_shift() removes from the front and has to reindex every remaining element, making it O(n). If the order of processing does not matter, popping is dramatically faster on large arrays.

When to reach for it

Use it for stack-shaped work: undo histories, path segment resolution, depth-first traversal worklists, or simply peeling the last item off a list you are consuming in a while loop. Because it both reads and removes in one step, it is safer than reading end($a) and then unsetting the key.

What trips people up

An empty array returns null without any warning, which is indistinguishable from popping a real null value. If that difference matters, check the count first or use array_key_last().

The parameter is by reference, so array_pop(explode(',', $s)) is an error — assign the result to a variable first. Finally, popping resets the internal array pointer and reindexes the remaining integer keys is not done: existing keys keep their values, only the last entry disappears.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Resolve '..' segments while normalising a path string
  • Drive a depth-first worklist until it is empty
  • Take the most recent entry off an undo history stack
  • Peel the file extension off an exploded filename

Frequently asked questions

What is the difference between array_pop and array_shift?

array_pop removes the last element and array_shift removes the first. array_pop is O(1) while array_shift has to reindex the whole array, so prefer popping when the processing order does not matter.

Does array_pop return null on an empty array?

Yes, and it does so silently with no warning or exception. Check that the array is non-empty first if you need to distinguish that from popping a stored null.

Why does array_pop say only variables should be passed by reference?

Because its parameter is by reference, it cannot accept the return value of another function directly. Assign the result to a variable and pop that variable instead.

More examples

Normalising a path with a stack

Each '..' pops the previous segment off the stack and '.' is skipped, so the output is usr/share/fonts. This is the canonical shape of stack work: push normal items, pop on a back-reference.

Example · php
<?php
$parts = [];
foreach (explode('/', 'usr/local/../share/./fonts') as $seg) {
    if ($seg === '..') {
        array_pop($parts);
    } elseif ($seg !== '' && $seg !== '.') {
        $parts[] = $seg;
    }
}
echo implode('/', $parts), "\n";

Draining a worklist

The loop condition tests the array itself rather than the popped value, so a legitimate null or 0 in the list would not end the loop early. The output is sum: 10 and an empty array.

Example · php
<?php
$queue = [1, 2, 3, 4];
$sum = 0;
while ($queue !== []) {
    $sum += array_pop($queue);
}
echo "sum: $sum\n";
var_dump($queue);

It must be given a variable

Writing array_pop(explode('/', $path)) directly would be an error because the by-reference parameter needs a variable to modify. Splitting it into two statements is the fix, and it leaves $segments holding the directory portion.

Example · php
<?php
$path = 'archive/report.tar.gz';
$segments = explode('/', $path);
$file = array_pop($segments);       // works: $segments is a variable
echo "dir: ", implode('/', $segments), "\n";
echo "file: $file\n";

Discussion

  • Be the first to comment on this lesson.