array_pop()

hP3188941580's avatar
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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PHP on Codecademy