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

This article explains how PHP's array_pop function removes and returns the last element of an array, demonstrates its syntax, provides a complete code example, and shows the resulting output, helping developers efficiently manipulate array data in backend applications.

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

In PHP, the array_pop function is a convenient built‑in utility for popping the last element off an array and returning it.

The function signature is: mixed array_pop ( array &$array ) If the array is empty, the function returns null. It modifies the original array by removing its final element.

Below is a full example that defines an array of fruit names, uses array_pop to extract the last fruit, and then prints both the popped value and the remaining array:

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

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

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

Thus, array_pop provides a simple way to handle the last element of an array, which is especially useful in backend data‑processing tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

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

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.