PHP array_pop() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_pop() removes the last item from an array and returns it.

The array becomes one item shorter. If the array is empty, it returns null.

Syntax

Call array_pop() with the values you need. Start with this line.

PHP
$last = array_pop($stack);
CallWhat it does
array_pop($stack)Removes and returns the last item of $stack.
$item = array_pop($list)Stores the removed item in $item.
array_pop($empty)Returns null when the array is empty.

array_pop() changes the original array. To only read the last value without removing it, use array_last() or array_key_last().

Library declarations
PHP
array_pop(array &$array): mixed

Parameters

ParameterDescription
arrayRequired. The array to remove the last item from. It is passed by reference.

Return Value

Return ValueDescription
mixedThe removed last value, or null if the array was empty.

PHP Examples

Example 1 — Remove the Last Item

Pop one item from a small list.

PHP
<?php
$stack = [1, 2, 3];

$removed = array_pop($stack);

echo $removed, PHP_EOL;
echo implode(", ", $stack), PHP_EOL;

How It Works

  1. Create a list. It holds 1, 2 and 3.
  2. Call array_pop(). It removes 3 from the end.
  3. Use the return value. The removed item is stored in $removed.
  4. Print the array. Only 1 and 2 are left.

Example 2 — Pop From an Empty Array

An empty array gives null instead of an error.

PHP
<?php
$empty = [];

$result = array_pop($empty);

echo var_export($result, true), PHP_EOL;
echo count($empty), PHP_EOL;

How It Works

  1. Create an empty array. There is nothing to remove.
  2. Call array_pop(). PHP returns null rather than showing an error.
  3. Print null safely. var_export() prints NULL.
  4. Count the array. It is still empty.

Example 3 — Use an Array as a Stack

Pop items one by one until nothing is left, last in first out.

PHP
<?php
$stack = ["a", "b", "c"];

while (($item = array_pop($stack)) !== null) {
    echo $item, PHP_EOL;
}

How It Works

  1. Create a stack. The last item added is c.
  2. Pop in a loop. Each call removes and returns the final item.
  3. Stop on null. When the array is empty, array_pop() returns null.
  4. Read the output. Items come out in reverse order.

Frequently Asked Questions

It removes the last item of an array and returns it. The array gets shorter by one.
It returns null, and the array stays empty.
Use array_shift(). It removes the first item and renumbers the keys.
Use array_push() or the short form $array[] = $value.
Use array_last() in PHP 8.5, or $array[array_key_last($array)] in earlier versions.

Next: array_product()

array_product() multiplies all the values of an array together and returns the result.

array_product() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful