The array_pop() function in PHP is used to remove the last element in the array and return the value of the element. This function can be widely used in array operations.
The following is how to use the array_pop() function:
Syntax
array_pop ( array &$array ) : mixed
Parameters
array: required. The array to operate on.
Return value
The array_pop() function returns the value of the removed element. If the array is empty, NULL is returned.
Example Demonstration
The following is a few simple examples to demonstrate the use of the array_pop() function.
$ages = array(20, 30, 40, 50); echo array_pop($ages); // 输出:50
$ages = array(20, 30, 40, 50); array_pop($ages); print_r($ages); // 输出:Array([0] => 20 [1] => 30 [2] => 40)
$ages = array(20, 30, 40, 50); $new_array = array(array_pop($ages)); print_r($new_array); // 输出:Array([0] => 50)
Notes
Summary
The array_pop() function is a common array operation function, which can play a big role in removing the last element in the array. Proficient in using the array_pop() function can enable developers to operate arrays more efficiently.
The above is the detailed content of Introduction to how to use PHP array_pop() function. For more information, please follow other related articles on the PHP Chinese website!