end
Returns the last element of an array.
Syntax
end(array|object &$array): mixedParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array|object | An array held in a variable. |
Return value
mixedThe last element, or `false` for an empty array.
Examples
The last element:
<?php
$numbers = [10, 20, 30];
echo end($numbers);Output:
30The same without the internal pointer:
<?php
$numbers = [10, 20, 30];
echo $numbers[array_key_last($numbers)];Output:
30Notes
The function takes only a variable: end(explode(',', $text)) raises a notice. It moves the array's internal pointer and leaves the array itself untouched.
See also
- reset — Returns the first element of an array.
- array_key_first — Returns the first key of an array.
- array_pop — Removes the last element of an array and returns it.
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.