array_pop()

Published Jul 8, 2023
Contribute to Docs

The array_pop() method removes the last element of an array, and returns the truncated array.

Syntax

array_pop($array)

The array_pop() function has one required parameter, $array, which specifies the input array. If the specified array is empty, null will be returned.

Example

The following example uses the array_pop() function to remove "Mexico" from the array $countries:

<?php
$countries = array("USA","Canada","England","Mexico");
array_pop($countries);
print_r($countries);
?>

The example will result in the following output:

Array (
[0] => USA
[1] => Canada
[2] => England
)

Codebyte Example

This example is runnable and uses the array_pop() function:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn PHP on Codecademy