PHP 8 reference · Array Functions
PHP array_last() Function
Return the last value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.
PHP 8 Since 8.5
Syntax
array_last(array $array): mixed
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
Return value
The last value, or null.
Examples
<?php
$path = ["home", "docs", "arrays"];
echo array_last($path), "\n";
Output
arrays
Run this on your computer — see “Get Started” for setup.
<?php
var_dump(array_last([]));
$path = ["home", "docs"];
echo end($path), "\n"; // the older way, which also moves the internal pointer
Output
NULL docs
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PHP 8.5 | Added |
In other languages
array.at()· JavaScript
Related
array_first()· Return the first value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.array_key_last()· Return the last key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.array_pop()· Remove the last element of an array and return it. Returns null if the array is empty.- PHP Arrays · tutorial
- PHP Array Functions · tutorial