W3docs

PHP Array Pop: The Ultimate Guide

Are you looking for a comprehensive guide on PHP Array Pop? Look no further! In this article, we will provide a thorough explanation of this useful PHP function

array_pop() removes the last element from an array, returns it, and shortens the array by one. It's how you treat a PHP array like a stack — the most recently added item is the first one you take back off. This page covers the syntax, what it returns, the side effects you need to know about (numeric re-keying and the internal pointer), and when to reach for it.

What array_pop() does

array_pop() is a built-in PHP function that:

  • Removes the element at the end of the array,
  • Returns that removed element's value, and
  • Modifies the array in place — the array passed in is permanently shortened.

If the array's keys are numeric (0, 1, 2, …), they are renumbered starting from 0 after the pop. String keys are left untouched. This is the same re-keying behaviour you get from array_shift(), and it's the main "gotcha" to remember.

Syntax

array_pop(array &$array): mixed
PartMeaning
&$arrayThe array to pop from, passed by reference — it is modified directly.
return valueThe value of the last element, or null if the array is empty.

Because $array is passed by reference, you cannot call array_pop() on a literal or a function result — you must pass a variable that holds an array.

Removing the last element

Pop the last item and capture the returned value:

php— editable, runs on the server

array_pop() removes "cherry", returns it, and leaves $fruits with two elements:

cherry
Array
(
    [0] => apple
    [1] => banana
)

Popping an empty array

If the array is already empty, array_pop() returns null. On modern PHP it does this silently, so you can safely check the result:

php— editable, runs on the server

Output:

Array is empty
No element to pop

Note that a returned null is ambiguous — you cannot tell "the array was empty" apart from "the last element really was null." When that distinction matters, check count($array) before popping.

Using array_pop() as a stack

Pair array_pop() with array_push() (or $array[] = ...) and you have a last-in, first-out (LIFO) stack. Looping with array_pop() drains the array from the end:

<?php

$stack = [1, 2, 3, 4, 5];

while (count($stack) > 0) {
    echo array_pop($stack) . " ";
}

?>

Output — the elements come back out in reverse order:

5 4 3 2 1 

Numeric keys are re-indexed

When you pop from an array with numeric keys and then add a new element with $array[], the new index continues from the re-numbered sequence, not the original maximum key:

<?php

$data = ["x" => 10, "y" => 20, "z" => 30];
$value = array_pop($data);   // removes "z" => 30

echo $value . "\n";          // 30
print_r($data);

?>
30
Array
(
    [x] => 10
    [y] => 20
)

String keys survive untouched. With numeric keys, array_pop() re-bases them to 0, 1, 2, …, which is something to watch for if other code relies on specific index values.

FunctionWhat it removes / adds
array_pop()Removes the last element.
array_push()Adds one or more elements to the end.
array_shift()Removes the first element.
array_unshift()Adds one or more elements to the front.
array_slice()Extracts a portion without modifying the original (when called normally).

For a broader refresher on how arrays work, see PHP Arrays.

Summary

  • array_pop() removes and returns the last element of an array, modifying it by reference.
  • An empty array yields null — use count() if you must distinguish empty from a real null value.
  • It's the natural tool for stack-style (LIFO) processing and for trimming an array's tail one item at a time.
  • Numeric keys are renumbered from 0; string keys are preserved.

Practice

Practice
What is the function of the array_pop() function in PHP?
What is the function of the array_pop() function in PHP?
Was this page helpful?