What is the best method to retrieve the value of the last element in a PHP array?

To retrieve the value of the last element in a PHP array, you can use the end() function to move the internal pointer to the last element of the array and then return that element. This method is efficient and straightforward, ensuring that you always get the last element regardless of the array's size.

$array = [1, 2, 3, 4, 5];
$lastElement = end($array);
echo $lastElement;