PHP array_pop() Function
Quick summary: The PHP array_pop() function removes the last element of an array and returns it.
PHP array_pop() Syntax
array_pop(array &$array): mixed
PHP
PHP array_pop() Basic examples
$stack = [1,2,3];
$last = array_pop($stack);
var_dump($last);
print_r($stack);
PHP
Output:
int(3) and Array ( [0] => 1 [1] => 2 )
Removes and returns the last element.
PHP array_pop() Real-world usage
$tasks = ['task1','task2','task3'];
$current = array_pop($tasks);
echo $current;
PHP
Output:
task3
Implements stack (LIFO) behavior.
PHP array_pop() Edge cases
$empty = [];
var_dump(array_pop($empty));
PHP
Output:
NULL
Returns null if the array is empty.
PHP array_pop() Common mistakes
Using on non-array variable
array_pop() expects an array.
Incorrect
array_pop('string');
Correct
array_pop($array);
Ensure the argument is an array.
PHP array_pop() Frequently Asked Questions
What does array_pop() do?
Removes and returns the last element of an array.
Use case?
Stack operations.
Modifies array?
Yes.
Return value?
Last element or null.
Common mistake?
Using on empty array.
Performance?
Fast.
Preserves keys?
Yes.
Safe?
Yes.
Alternative?
array_slice().
Handles empty array?
Returns null.
Used in stacks?
Yes.
Best practice?
Check array before pop.