end()

end() — Set the internal pointer of an array to its last element.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

end() advances array's internal pointer to the last element, and returns its value.

Signature

end(array|object &$array): mixed

Parameters

ParameterTypeWhat it takes
&$arrayarray|objectThe array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

Return value

Returns the value of the last element or false for empty array.

Example

A runnable snippet:

$fruits = array('apple', 'banana', 'cranberry'); echo end($fruits); // cranberry

Changelog

VersionChange
8.1.0Calling this function on objects is deprecated. Either convert the object to an array using get_mangled_object_vars() first, or use the methods provided by a class that implements Iterator, such as ArrayIterator, instead.
7.4.0Instances of SPL classes are now treated like empty objects that have no properties instead of calling the Iterator method with the same name as this function.

See also

current(), prev(), reset(), next(), array_key_last()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page end() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.