end
Moves an array's internal pointer to the last element and returns that element's value.
end(array|object &$array): mixed- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array|object | The array to operate on. Passed by reference because the internal pointer is moved. |
Return value
mixedThe value of the last element, or false if the array is empty.
Errors
Does not throw. Emits a notice when passed a value that cannot be taken by reference, and a deprecation notice since PHP 8.1.0 when called on an object.
Changelog
- PHP 8.1.0Calling this function on objects is deprecated. Convert the object with get_mangled_object_vars() first, or use a class implementing Iterator such as ArrayIterator.
- PHP 7.4.0Instances of SPL classes are now treated like empty objects with no properties instead of calling the Iterator method of the same name.
end jumps the array's internal pointer to the last element and hands you its value. It is the fastest way to read the tail of an array whose last key you do not know — including a list whose indexes have holes after an unset.
When to reach for it
The most common use is picking the last piece of a split string: end(explode('.', $filename)) for a file extension, or the final segment of a path. It is also handy for reading the newest entry in an append-only log array.
What trips people up
The parameter is by reference, so you cannot pass a function's return value directly. end(explode('.', $name)) raises a notice on PHP 8 because explode's result is a temporary — assign it to a variable first, or use array_key_last, which takes its argument by value.
The side effect is easy to forget: after end, the pointer sits on the last element, so a later current or a manual next/prev loop over the same array starts from the wrong place. Call reset when you are done.
An empty array returns false, which is indistinguishable from a stored false. In PHP 7.3 and later, array_key_last answers the same question without moving the pointer and returns null for an empty array — prefer it in new code.
Example
When to use it
- Read a file extension from the last segment of an exploded filename.
- Fetch the most recent entry from an array used as an append-only log.
- Get the final value of an array whose keys have gaps after unset calls.
Frequently asked questions
How do I get the last element of an array in PHP?
Use end($array) for the value, or array_key_last($array) if you want the key or need to leave the internal pointer alone. Both work with non-sequential keys.
Why does end() give me "Only variables should be passed by reference"?
You passed a function call or expression instead of a variable, and end takes its argument by reference so it can move the pointer. Assign the array to a variable first, or switch to array_key_last.
Does end() remove the last element?
No. It only moves the internal pointer and returns the value; the array is unchanged. Use array_pop if you want to remove and return the last element.
More examples
end versus array_key_last
Both find the last element even though the integer keys are out of order, because array order is insertion order and not key order. array_key_last is preferable when you also need the key or want to avoid the pointer side effect.
<?php
$rows = [10 => 'a', 4 => 'b', 99 => 'c'];
echo end($rows), "\n"; // c, and moves the pointer
reset($rows);
$last = array_key_last($rows);
echo "$last => {$rows[$last]}\n"; // 99 => c, pointer untouchedWhy the reference matters
Assigning the result to $parts gives end() a real variable to take a reference to, so it prints index.php cleanly. Passing the call directly would still work but would litter the log with a notice on every request.
<?php
function segments(string $path): array {
return explode('/', $path);
}
$parts = segments('/var/www/html/index.php'); // store it first
echo end($parts), "\n"; // index.php
// end(segments(...)) would emit "Only variables should be passed by reference"Empty arrays return false
end() gives an identical bool(false) for an empty array and for an array whose last element is false, so it cannot distinguish the two cases. array_key_last returns NULL only for the genuinely empty array, which is why it is the safer emptiness test.
<?php
$empty = [];
$falsy = [false];
var_dump(end($empty)); // bool(false)
var_dump(end($falsy)); // bool(false) — same result!
var_dump(array_key_last($empty)); // NULL
var_dump(array_key_last($falsy)); // int(0)
Discussion