What is the array end() method in PHP?

The end method is used to set the position of the internal pointer of the array to point to the last element of the array and return its value.

In PHP, each array contains an internal pointer that points to the current element. It initially points to the first element of the array. We can move the pointer positions using methods like next, prev, etc.

Syntax

end(array|object &$array): mixed

If an array is empty, then false is returned.

Example

<?php
$numbers = [1,2,3,4,5];
echo "Current Value is : ". current($numbers). "\n";
echo "Moving to the end of the array : ". end($numbers). "\n";
echo "Current Value is : ". current($numbers). "\n";
?>

In the code above:

  • We created a numbers array.

  • We printed the value of the current element pointed by the internal pointer using the current method.

  • We used the end method to move the pointer position to the end of the array and printed the value.

  • We again printed the current value pointed by the internal pointer of the array using the current method.