PHP array_pop Function
last modified March 13, 2025
The PHP array_pop
function removes and returns the last element
of an array. It reduces the array length by one and returns the removed value.
Basic Definition
The array_pop
function removes the last element from an array.
It returns the removed element or null if the array is empty.
Syntax: array_pop(array &$array): mixed
. The function takes an
array by reference and modifies it directly. The array's length decreases by 1.
Basic array_pop Example
This demonstrates removing the last element from a simple numeric array.
<?php $fruits = ['apple', 'banana', 'cherry']; $lastFruit = array_pop($fruits); print_r($fruits); echo $lastFruit;
The code removes 'cherry' from the array and stores it in $lastFruit. The original array now contains only two elements.
Working with Numeric Arrays
This example shows how array_pop affects numeric array indexes.
<?php $numbers = [10, 20, 30, 40]; $lastNumber = array_pop($numbers); echo "Removed: $lastNumber\n"; echo "Count: " . count($numbers) . "\n"; print_r($numbers);
The last element (40) is removed and returned. The array's count decreases from 4 to 3, but existing indexes remain unchanged.
Using array_pop with Associative Arrays
Demonstrates array_pop behavior with associative arrays.
<?php $user = [ 'name' => 'John', 'email' => 'john@example.com', 'age' => 30 ]; $lastValue = array_pop($user); echo "Removed: $lastValue\n"; print_r($user);
The last key-value pair ('age' => 30) is removed. The function works the same way with associative arrays as with indexed arrays.
Empty Array Behavior
Shows what happens when array_pop is called on an empty array.
<?php $emptyArray = []; $result = array_pop($emptyArray); var_dump($result); echo count($emptyArray);
When called on an empty array, array_pop returns NULL. The array remains empty and no error is generated.
Using array_pop in a Loop
Demonstrates processing array elements by repeatedly removing the last one.
<?php $stack = ['first', 'second', 'third']; while ($element = array_pop($stack)) { echo "Processing: $element\n"; } // Processing: third // Processing: second // Processing: first
The loop removes and processes each element from the end until the array is empty. This is a common pattern for stack-like operations.
Best Practices
- Check array length: Verify array isn't empty if NULL is problematic.
- Preserve original: Copy array first if you need the original later.
- Stack operations: Use with array_push for stack functionality.
- Performance: Faster than array_shift for large arrays.
Source
This tutorial covered the PHP array_pop
function with practical
examples showing its usage for removing array elements.
Author
List all PHP Array Functions.