Simplify Your PHP Code: Native array_first() & array_last() – Coming in PHP 8.5
Introduction
PHP 8.5, scheduled for release in November 2025, is set to significantly streamline array handling with the introduction of two new native functions: array_first() and array_last(). These additions address longstanding developer frustrations and enhance code clarity.
Why These Functions Matter
Prior to PHP 8.5, developers had to rely on various workarounds to fetch the first or last value in an array:
Using reset() or end(), which modify the internal array pointer and can lead to unexpected side‑effects.
Combining array_key_first()/array_key_last() (added in PHP 7.3) with manual indexing—for example, $array[array_key_first($array)]—which works, but isn't elegant.
Employing bulky userland code like array_pop, array_reverse, or framework helpers like Laravel’s Arr::first() and Arr::last().
All of these approaches either clutter the code or risk altering the array state.
Enter array_first() & array_last()
PHP 8.5 introduces two clean, intuitive functions:
function array_first(array $array): mixed {} function array_last(array $array): mixed {}Both functions return the first or last value directly without changing the internal pointer.
If the array is empty, they return null.
They work perfectly with associative arrays (e.g., ['a' => 'foo', 'b' => 'bar']), returning 'foo' or 'bar' respectively.
Usage Examples
array_first([10, 20, 30]); // returns 10array_last([10, 20, 30]); // returns 30
array_first(['x' => 'red', 'y' => 'blue']); // 'red'
array_last(['x' => 'red', 'y' => 'blue']); // 'blue'
array_first([]); // null array_last([]); // null
What RFCs and Polyfills Say
The official RFC for array_first_last is merged into the PHP source, so the implementation is locked in for PHP 8.5.
Meanwhile, frameworks like Laravel and Symfony already provide equivalent helpers:
Laravel:
Arr::first($array, $callback = null, $default = null) iterates using foreach and returns the first element or default.
Arr::last(...) uses end() when no callback is provided and reverses the array when a callback is used.
Symfony Polyfill:
Implements array_first() via foreach, returning null for empty arrays.
array_last() relies on current(array_slice($array, -1)), also yielding null if empty.
These solutions are now officially becoming part of PHP core, making custom helpers and polyfills mostly obsolete.
What This Means for Your Code
Cleaner syntax: No more juggling array pointers or complex helper calls—just call array_first() or array_last().
Performance: Native implementations are faster and safer than userland alternatives.
Backward compatibility: Developers need to be cautious—defining these functions manually in older versions could conflict with PHP 8.5’s native definitions. Namespacing or conditional definitions can help.
Looking Ahead
As PHP 8.5 approaches its November 2025 release, it’s a great time to start planning to adopt these functions. Audit existing codebases to remove polyfills and update array handling patterns. The result? More readable, maintainable and bug-resistant PHP code.
Conclusion
array_first() and array_last() in PHP 8.5 are small but mighty additions—bringing simplicity, clarity, and reliability to a common task. Your next refactor or feature build just got a little easier. Keep your PHP sharp and code smarter!