In PHP, end() and array_pop() are both used to manipulate arrays. They seem a bit similar, but actually have some important differences. This article will introduce the usage of the two, compare their differences, and their respective effects.
The end() function is used to move the internal pointer of the array to the last element of the array and return the value of that element. It will not change the structure of the original array, it will only modify the pointer position inside the array.
end($array);
parameter :
$array : an array, end() will point to the last element of this array.
Return value :
Returns the value of the last element of the array. If the array is empty, FALSE is returned.
$array = [10, 20, 30, 40];
$lastElement = end($array);
echo $lastElement; // Output:40
Move pointers only : end() just moves the array pointer to the last element, but does not remove any elements from the array.
The array structure has not changed : after calling end() , the array itself still exists and the elements are not deleted.
The array_pop() function is used to pop (delete) the last element from the array and return the value of that element. Unlike end() , array_pop() will modify the original array and delete the last element.
array_pop($array);
parameter :
$array : an array, array_pop() will remove and return the last element of the array.
Return value :
Returns the value of the deleted element. If the array is empty, return NULL .
$array = [10, 20, 30, 40];
$lastElement = array_pop($array);
echo $lastElement; // Output:40
print_r($array); // Output:[10, 20, 30]
Modify the array structure : array_pop() will delete the last element from the array, so it will change the structure of the original array.
It will affect the length of the array : after deleting the element, the length of the array will decrease.
characteristic | end() | array_pop() |
---|---|---|
Operation method | Move the pointer to the last element of the array | Delete the last element of the array and change the array structure |
Return value | Returns the value of the last element without changing the array structure | Return and delete the last element, change the array structure |
Is the array structure changed? | Don't change the array structure | Change the array structure and reduce the array length |
Return value type | If the array is empty, return FALSE | If the array is empty, return NULL |
end() is suitable for scenarios where only the last element of the array is required to access, and does not change the structure of the array. It is a function that operates on array pointers, returning the value of the last element, but not removing that element.
array_pop() is suitable for scenes where the last element needs to be popped from an array, and will delete the element and return its value.
Although they all have access to the last element of the array, they operate differently and affect differently. When choosing to use, developers need to decide whether to use end() or array_pop() according to their specific needs.
I hope that through the introduction of this article, we can help you understand the differences between these two functions and their respective usage scenarios more clearly.