Backend Development 6 min read

Introducing PHP 8.5’s array_first() and array_last() Functions

PHP 8.5 adds two handy functions, array_first() and array_last(), which retrieve the first and last elements of an array without altering the internal pointer, offering clearer syntax, safety, and performance benefits, along with usage examples, compatibility notes, and recommended scenarios.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Introducing PHP 8.5’s array_first() and array_last() Functions

PHP 8.5 adds two handy functions, array_first() and array_last() , which simplify accessing the first and last elements of an array without changing the internal pointer, making code clearer and more concise. These functions complement PHP’s array toolkit and are especially useful in scenarios that frequently manipulate array endpoints.

Why these functions are needed

In current PHP versions, obtaining the first or last element of an array usually requires one of the following approaches:

// Get first element
$first = reset($array); // changes internal pointer
$first = $array[array_key_first($array)]; // PHP 7.3+

// Get last element
$last = end($array); // changes internal pointer
$last = $array[array_key_last($array)]; // PHP 7.3+

These methods either modify the array’s internal pointer or involve verbose syntax that is not intuitive. The introduction of array_first() and array_last() resolves these issues.

Function usage

array_first()

array_first(array $array): mixed

array_first() returns the first element of the array without altering the internal pointer. If the array is empty, it returns null .

Example:

$numbers = [10, 20, 30, 40];
echo array_first($numbers); // outputs 10

$empty = [];
var_dump(array_first($empty)); // outputs NULL

array_last()

array_last(array $array): mixed

array_last() returns the last element of the array, also without changing the internal pointer. It returns null for an empty array.

Example:

$colors = ['red', 'green', 'blue'];
echo array_last($colors); // outputs 'blue'

$empty = [];
var_dump(array_last($empty)); // outputs NULL

Advantages and applicable scenarios

Code readability: function names clearly express intent, more intuitive than index or pointer operations.

Safety: does not change the array’s internal pointer, avoiding side effects.

Conciseness: a single line accomplishes the task without temporary variables.

Empty‑array handling: uniformly returns null , simplifying error handling.

Especially suitable for:

Processing queue or stack data structures.

Retrieving the newest or oldest records.

Checking whether an array contains any elements.

Performance considerations

These functions are optimized and have performance comparable to combining array_key_first() / array_key_last() , but are slightly slower than reset() / end() because they perform an additional empty‑array check. In practice, the difference is negligible.

Backward compatibility

Developers needing similar functionality on older PHP versions can define compatible polyfills:

if (!function_exists('array_first')) {
    function array_first(array $array) {
        return $array[array_key_first($array)] ?? null;
    }
}
if (!function_exists('array_last')) {
    function array_last(array $array) {
        return $array[array_key_last($array)] ?? null;
    }
}

Conclusion

array_first() and array_last() are small yet useful additions in PHP 8.5 that provide clearer, safer syntax for accessing array endpoints, improving code readability and maintainability. Every PHP developer should become familiar with these new tools.

Promotional links: Java learning resources , C language learning resources , Frontend learning resources , C++ learning resources , PHP learning resources .

Backend DevelopmentPHParray_firstarray_lastphp8.5
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.