Array operations refer to the various manipulations that can be performed on arrays, such as accessing, modifying, adding, removing, sorting, and searching elements. Arrays are a fundamental data structure in computer programming and are used extensively in algorithms, data structures, and applications.
PHP pop() Function
The following depicts the syntax for the
pop() function in PHP.
array_pop(array &$array): mixed
The function accepts only one parameter:
array- the target array whose last element you wish to delete.
The function removes the last element in the array and return it to the caller.
NOTE: The function will
reset() the array pointer of the input array after use.
pop() function usage.
// Create an array $fruits = array("apple", "banana", "orange", "grape"); // Remove the last element from the array $last_fruit = array_pop($fruits); // Output the removed element and the updated array echo "Removed element: " . $last_fruit . "<br>"; print_r($fruits);
In this example, the
array_pop() function is used to remove the last element
"grape" from the
$fruits array and store its value in the
$last_fruit variable. The output would be:
Removed element: grape Array (  => apple  => banana  => orange )
As you can see, the
$fruits array no longer contains the
"grape" element, which has been removed by the
Using this tutorial, you learned how to remove and return the last element from an array in PHP. Check our other PHP tutorials to learn more.