Backend Development 4 min read

Using PHP’s array_pop Function to Remove the Last Element from an Array

This article explains the PHP array_pop function, detailing its syntax, behavior of removing and returning the last element of an array, provides a complete code example with output, and demonstrates how to use it effectively for array manipulation.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP’s array_pop Function to Remove the Last Element from an Array

In PHP we often need to manipulate array data. To retrieve and remove the last element, PHP provides the convenient array_pop function.

Syntax of array_pop

<code>mixed array_pop ( array &$array )</code>

The function accepts an array by reference, removes its last element, and returns that element; if the array is empty it returns null.

Code example

<code>&lt;?php
// Define an array
$fruits = array("apple", "banana", "orange", "grape");

// Use array_pop to pop and return the last element
$last_fruit = array_pop($fruits);

// Output the popped element
echo "Popped element is: " . $last_fruit . "\n";

// Output the remaining array
echo "Remaining array:\n";
print_r($fruits);
?&gt;</code>

The code defines an array $fruits containing several fruit names, uses array_pop to remove the last element ("grape") and assigns it to $last_fruit , then prints the popped value and the remaining array.

Running the script produces the following output:

<code>Popped element is: grape
Remaining array:
Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
</code>

Thus, array_pop easily removes and returns the final element of an array, which is useful for array manipulation in PHP.

PHP learning recommendations

Vue3+Laravel8+Uniapp beginner to practical development tutorial

Vue3+TP6+API social e‑commerce system development tutorial

Swoole from beginner to mastery recommended course

Workerman+TP6 instant messaging chat system limited‑time offer

backendPHPCode Examplearray-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.