last

Returns the last element of an array, or false when the array is empty.

Signature
last(array $array): mixed

Parameters

ParameterTypeDescription
$$arrayarrayThe array to read the final element from.

Return value

mixedThe final element of the array, or false if the array is empty.

Errors

Passing a non-empty value that is not an array raises a TypeError from the underlying array_last() call.

Changelog

  • PHP 12.26.0Reimplemented as empty($array) ? false : array_last($array); previously it called end($array). The empty-array result is still false.

last() returns the final value of an array without you having to know its key. It is the mirror of head(), and unlike end() it accepts an expression rather than requiring a variable it can take a reference to, so you can feed it the return value of another call directly.

When to reach for it

Use it when the array is a plain list and you only want the tail: the extension on the end of an exploded filename, the most recent entry appended to a log array, the final segment of a URI path. If you are holding a Collection, call its own last() method instead — the global helper takes arrays only and will not accept a collection.

What trips people up

An empty array returns false, not null. That matters because false is also a perfectly legal array value, so last($flags) returning false tells you nothing about whether the array was empty. Compare against [] first when the distinction matters, or use Arr::last($array, null, $default), which lets you name a fallback that cannot be confused with data.

The helper also has no callback form. Finding the last matching element is Arr::last()'s job.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Pull the file extension off the end of an exploded filename.
  • Read the most recent entry from an array you have been appending to.
  • Grab the last segment of a URI path split on slashes.

Frequently asked questions

Why does last() return false instead of null on an empty array?

Because false is the historical return of PHP's end() on an empty array, and Laravel kept that contract when it moved to array_last() in 12.26.0. Since false can also be a legitimate element, check $array === [] beforehand, or use Arr::last($array, null, $default) to supply a fallback you can recognise.

What is the difference between last() and Arr::last()?

Arr::last() adds two things the helper has not got: an optional truth-test callback, so you can find the last element matching a condition, and an explicit default returned when nothing matches or the array is empty. The global last() always returns the final element or false.

Does last() move the array's internal pointer?

No. Since Laravel 12.26.0 it is implemented with array_last(), which does not touch the pointer, and even the earlier end()-based version only moved the pointer of its own by-value copy, never the caller's array.

More examples

Telling an empty array apart from a false element

Both calls to last() print bool(false) for completely different reasons, which is why the return value cannot be used as an emptiness test. Arr::last([], null, 'none') prints string(4) "none" instead, so the fallback is distinguishable from any element the array could have held.

Example · php
<?php

use Illuminate\Support\Arr;

$flags = [true, false];

var_dump(last($flags));   // bool(false) — a real value
var_dump(last([]));       // bool(false) — the empty marker

// Arr::last lets you name a fallback that cannot be confused with data.
var_dump(Arr::last([], null, 'none'));

Taking the tail of an expression

The third line is the practical reason to prefer last() over end(): array_filter() returns a value, not a variable, so end() would need a temporary. Note that array_filter preserves keys — the result is [1 => 2, 3 => 4] — and last() still returns 4 because it reads the final element, not element [1].

Example · php
<?php

$path = 'invoices/2024/march/summary.pdf';

echo last(explode('/', $path)), "\n";        // summary.pdf
echo last(explode('.', 'archive.tar.gz')), "\n"; // gz

// last() takes an expression; end() would need a variable first.
echo last(array_filter([1, 2, 3, 4], fn (int $n) => $n % 2 === 0)), "\n"; // 4

Collections have their own last()

Collection::last() accepts an optional callback and returns the last element passing it, which the global helper cannot do. Passing a Collection straight to last() fails, so call ->all() first if you really want the helper.

Example · php
<?php

$collection = collect([100, 200, 300]);

echo $collection->last(), "\n";                       // 300

// The method form takes a truth test; the helper does not.
echo $collection->last(fn (int $n) => $n < 300), "\n"; // 200

// The helper only accepts arrays.
echo last($collection->all()), "\n";                   // 300

Discussion

  • Be the first to comment on this lesson.