In PHP, the end() function is a very common function that points an internal pointer to the last element and returns the value of that element. Simply put, the end() function allows you to quickly access the last element in the array.
end(array $array): mixed
array : Must be an array.
Return value: Returns the value of the last element in the array. If the array is empty, return false .
A PHP array is an ordered collection. Each element in the array has a pointer to its location. By default, the array pointer points to the first element of the array. When the end() function is called, PHP will move the array pointer to the last element and return the value of that element.
It should be noted that the end() function does not change the structure of the array, it only operates on the position of the pointer inside the array.
<?php
$arr = [1, 2, 3, 4, 5];
$lastElement = end($arr); // return 5
echo $lastElement;
?>
In the example above, the end() function returns the last element in the array $arr , which is 5 . At this time, the array pointer will point to 5 , but the structure of the array itself has not been changed.
<?php
$arr = [];
$lastElement = end($arr); // return false
echo $lastElement; // Output false
?>
If the array is empty, the end() function returns false , indicating that the array does not have the last element.
Sometimes, we may need to get the last element of the array and do further operations. For example, we can use the end() function in conjunction with the key() function to get the key value of the last element.
<?php
$arr = ['a' => 1, 'b' => 2, 'c' => 3];
$lastElement = end($arr); // Get the last element
$lastKey = key($arr); // Get the last element的键
echo "The key of the last element is:$lastKey,The value is:$lastElement";
?>
Output:
The key of the last element is:c,The value is:3
Position of array pointer : Using the end() function will change the position of the internal pointer of the array. After that, if you need to revisit other elements of the array, you may need to use the reset() function to reset the pointer to the first element.
Processing of associative arrays : The end() function handles associative arrays like ordinary arrays, returning the value of the last element, not the last key value.
Reference pass : end() returns the value of the array element. If you need to modify the last element in the array, you can do it by passing the reference.
<?php
$arr = [10, 20, 30];
$lastElement = end($arr); // Get the last element
$lastElement = 100; // Modify the value of the last element
echo $arr[2]; // Output 100,Because the reference pass changes the original array
?>
The end() function is a very practical array operation function in PHP, which helps us quickly access the last element of the array. Although it does not change the structure of the array itself, it moves the internal pointer of the array. Therefore, end() is a very convenient tool when it comes to processing the end elements of an array. Understanding how to use this function can make you more efficient when processing arrays.