← All posts

PHP 8.5 array_first() and array_last(): A Practical Guide for Real Code

How PHP 8.5's array_first() and array_last() work, where they beat reset(), end() and [0], the null catch, and a safe fallback for PHP 8.4.

Ian at a desk with a laptop, brown tabby cat beside him, soft natural light

Most PHP codebases have a small museum of ways to grab the first or last item in an array. reset(). end(). $rows[0]. $rows[count($rows) - 1]. A foreach with a break that someone wrote at 11pm.

PHP 8.5 added two functions that make all of that unnecessary: array_first() and array_last(). Here is how they behave, where they help, and the one thing to watch.

What the functions return

From the PHP manual:

  • array_first(array $array): mixed returns the first value of the array, or null if it is empty.
  • array_last(array $array): mixed returns the last value, or null if it is empty.

"First" and "last" mean the order the array is stored in, not the numeric keys:

$bookings = [3 => 'Mon 09:00', 0 => 'Tue 13:00', 7 => 'Fri 09:00'];

var_dump(array_first($bookings)); // string(9) "Mon 09:00"

var_dump(array_last($bookings)); // string(9) "Fri 09:00"

var_dump($bookings[0]); // string(9) "Tue 13:00"

If you have ever used $rows[0] after an array_filter() and got an "Undefined array key 0" warning, this is why. Filtering keeps the original keys.

Replacing the usual patterns

The PHP 8.5 release notes give this before-and-after:

PHP 8.4 and older:

$lastEvent = $events === [] ? null : $events[array_key_last($events)];

PHP 8.5:

$lastEvent = array_last($events);

Other common swaps:

  • reset($rows) becomes array_first($rows)
  • end($rows) becomes array_last($rows)
  • $rows[array_key_first($rows)] ?? null becomes array_first($rows)

Why bother changing working code? Because reset() and end() move the array's internal pointer, take the array by reference (so reset(loadRows()) raises an "Only variables should be passed by reference" notice), and return false on an empty array. The new functions do none of those things.

A small business example

Say you store status changes for an order, oldest first, and want the current status and the date it was first opened:

$history = [['status' => 'quoted', 'at' => '2026-11-02'], ['status' => 'booked', 'at' => '2026-11-03'], ['status' => 'completed', 'at' => '2026-11-05']];

$current = array_last($history)['status'] ?? 'new';

$opened = array_first($history)['at'] ?? null;

echo $current; // completed

With an empty history, array_last() returns null, the ?? 'new' kicks in, and nothing warns or breaks.

Combine it with array_filter() to get "the first one that matches":

$firstUnpaid = array_first(array_filter($invoices, fn(array $i): bool => !$i['paid']));

If you are on PHP 8.4 or newer, array_find() does the same in one call and stops at the first match, which is better for big arrays. array_first(array_filter(...)) is handy when you want the filtered list anyway.

The catch: null as a real value

An empty array and an array whose first value is null both give you null:

var_dump(array_first([])); // NULL

var_dump(array_first([null, 'b'])); // NULL

If null can legitimately sit in your data, check emptiness explicitly:

if ($readings === []) { throw new RuntimeException('No meter readings recorded.'); }

$first = array_first($readings);

Also, these functions only accept arrays. Passing a generator throws a TypeError. Use iterator_to_array() first if you genuinely need the values in memory.

A fallback for PHP 8.4 and older

If you maintain code that has to run on older versions, add a guarded polyfill. When the server moves to 8.5, the built-in version is used automatically:

if (!function_exists('array_first')) { function array_first(array $array): mixed { return $array === [] ? null : $array[array_key_first($array)]; } }

if (!function_exists('array_last')) { function array_last(array $array): mixed { return $array === [] ? null : $array[array_key_last($array)]; } }

array_key_first() and array_key_last() have been in PHP since 7.3, so this works on anything you should still be running. If that still includes PHP 8.2, its security support ends on 31 December 2026, according to the supported versions page.

Quick checklist

  1. Search the codebase for reset(, end( and [0] used to mean "first".
  2. Swap in array_first() or array_last() where you only need the value.
  3. Keep an explicit === [] check wherever null is valid data.
  4. Add the polyfill if any server is still below 8.5.

Small change, fewer quiet bugs, and the next person reading the code doesn't need to remember what the pointer was doing.

Where to go next

PHP Done The Correct Way covers modern PHP habits like this with tested examples: paperback (UK) or Kindle (UK). The companion code page has the free downloads.

Want a hand bringing an older PHP codebase up to date? See PHP AIR services. More from Ian on his blog.

Let's talk More posts