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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_last(array $array): mixed

Parameters

ParameterDescription
array The input array.

Return value

The last value, or null.

Examples

Last value
<?php
$path = ["home", "docs", "arrays"];
echo array_last($path), "\n";

Output

arrays

Run this on your computer — see “Get Started” for setup.

Empty arrays and end()
<?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

VersionChange
PHP 8.5Added

In other languages

Was this page helpful?