Backend Development 3 min read

Using PHP's array_pop Function to Remove and Return the Last Element of an Array

This article explains how PHP's array_pop function works, shows its syntax, provides a complete code example that pops the last element from an array, displays the removed value and the remaining array, and summarizes its usefulness for array manipulation.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_pop Function to Remove and Return the Last Element of an Array

In PHP, handling array data often requires extracting the last element for further processing, and the built‑in array_pop function offers a convenient way to do this.

The array_pop function removes the final element from an array, returns that element, and returns null when the array is empty. Its syntax is:

mixed array_pop ( array &$array )

A practical example defines an array of fruit names, calls array_pop to pop the last fruit, and then prints both the popped value and the remaining array:

<?php // Define an array $fruits = array("apple", "banana", "orange", "grape"); // Pop and return the last element $last_fruit = array_pop($fruits); // Output the popped element echo "弹出的元素是:" . $last_fruit . "\n"; // Output the remaining array echo "剩余的数组是:\n"; print_r($fruits); ?>

Running this script produces the following output, showing that "grape" was removed and the array now contains only the first three fruits:

弹出的元素是:grape 剩余的数组是: Array ( [0] => apple [1] => banana [2] => orange )

Thus, array_pop provides a simple and effective method for retrieving and removing the last element of an array, which is especially useful in many backend data‑processing scenarios.

backendtutorialarray-manipulationarray_pop
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.