Exploring array_first() and array_last() Functions in PHP 8.5

PHP 8.5 array_first, PHP 8.5 array_last, new PHP array functions

Alright, lemme tell you something that made me weirdly happy in the middle of a boring Tuesday — PHP 8.5 finally gave us array_first() and array_last().
Yep, after years of duct-taping our way around arrays, we now have official, native functions to grab the first and last elements.
And I know what you’re thinking: “Wait, wasn’t this always easy to do?”

Well… yes and no. Let me break it down from a dev who’s been there (and probably overused reset() more times than I should admit).


Before PHP 8.5: How We Grabbed First and Last Elements (A.K.A. The Hack Era)

So back in the day — aka like last month — the way we accessed the first item in an array usually went like this:

$first = reset($myArray);

And the last one?

$last = end($myArray);

Simple, right?

Well, not quite. The catch is: both reset() and end() actually move the internal pointer of the array.
That means if you’re looping over it later using foreach, or doing some other sequence-based stuff, you might get unexpected results.
(Yes, I learned this the hard way. Twice.)

Then came PHP 7.3 with array_key_first() and array_key_last() — which helped a bit:

$first = $arr[array_key_first($arr)];
$last = $arr[array_key_last($arr)];

Clean? Not really. That syntax just feels clunky — like we’re asking PHP to do something it clearly should’ve made easier.


Enter: array_first() and array_last() in PHP 8.5

So finally in PHP 8.5, we get these two:

array_first($array);
array_last($array);

And… that’s it. No messing around with pointers, no grabbing keys, no nesting nonsense.
Just a straight, “Hey PHP, give me the first thing,” and PHP finally goes, “Sure bro.”

Example:

$fruits = ['apple', 'banana', 'cherry'];

echo array_first($fruits); // apple
echo array_last($fruits);  // cherry

It’s honestly the kind of tiny quality-of-life update that just feels good.


What About Empty Arrays?

Good question. If the array is empty, both functions will return null. Not false, not throw an error — just a calm, silent null.

$empty = [];

array_first($empty); // null
array_last($empty);  // null

That’s useful, but also — watch out.
Because null could also be an actual value in your array. So maybe wrap it in a type check or condition if it matters for your logic.


Works With Associative Arrays Too? Yup.

I was curious about this too. So I tried:

$data = [
    'name' => 'Andi',
    'role' => 'Developer',
    'location' => 'Bandung'
];

echo array_first($data); // Andi
echo array_last($data);  // Bandung

And it works just fine. It respects the order of insertion, not the key names.
Perfect for when you’re dealing with API response data, or JSON decoded arrays, where the order matters.


Why This Feels Like a Big Deal (Even Though It’s Small)

Let’s be real — this isn’t a groundbreaking feature.
It’s not going to change the way you architect systems or make your app 10x faster.

But it reduces mental clutter.
You no longer have to pause and think, “Wait, what’s the cleanest way to get the last value again?” or write $arr[array_key_last($arr)] six times in a row.

Plus, for beginners, it just reads better.

array_first($something);

That makes more sense than reset() — which doesn’t really tell you what it’s doing unless you already know.


Backward Compatibility Warnings 🚨

If you’re like me and had a helper function named array_first() from a legacy codebase or an old Laravel version… yeah, this might be a problem now.

Because array_first() is now a native PHP function, your app will probably throw a fatal error if that name clashes.

What to do?

  • Rename your custom function, obviously.
  • Or check if your project even needs that helper anymore — because hey, now it’s native 🎉

Using It in Real Projects

Since I updated to PHP 8.5, I already replaced like 10 helper methods with native array_first() and array_last().

In one use case, I was grabbing the first error from a validation failure to show in a toast:

$firstError = array_first($validator->errors()->all());

Looks so clean. Reads like English. No hacky key-first combos anymore.


What If You’re Stuck on PHP < 8.5?

No problem. You can polyfill it like this:

function array_first(array $array): mixed {
    return $array === [] ? null : reset($array);
}

function array_last(array $array): mixed {
    return $array === [] ? null : end($array);
}

Put that in your helper file, and you’re good.
Or, install a package like symfony/polyfill-php85 (eventually someone will build one).


Final Thoughts

PHP has come a long way — and features like array_first() and array_last() show that the core team still cares about making life easier for developers.

No, it’s not some big “framework-level” change. But for people like us who read and write PHP daily, these small touches add up.

I used to write little helper functions like these myself.
Now? I just type array_last($data) and move on with my life.

Simple. Clean. Very PHP 8.5 energy.


Pro Tip:
If you ever doubt whether a language update matters, ask yourself:

“Does this remove 3 lines of code I’ve been repeating for years?”

If the answer is yes — you bet it’s worth it.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top