First And Last Array Values In PHP 8.5

Requires PHP 8.5 or newer for array_first() and array_last().

After working through Arrays, you can use two small helpers to read the ends of an array. array_first() returns its first value, and array_last() returns its last value. Both return null for an empty array. The PHP manual documents array_first() and array_last() as PHP 8.5 additions.

Read the first and last values

Imagine a delivery moving through a sequence of statuses. You want to display where it started and where it finished while keeping the complete history available.

PHP example
<?php

declare(strict_types=1);

$statuses = ['received', 'packed', 'dispatched'];

echo array_first($statuses) . PHP_EOL;
echo array_last($statuses) . PHP_EOL;
echo count($statuses) . PHP_EOL;

// Prints:
// received
// dispatched
// 3

Neither call removes an element. This makes the helpers useful when a page needs a short summary as well as the full list. If you need to remove the first or last item as part of processing it, that is a different operation: array_shift() and array_pop() change the array.

Array order determines the result

The functions follow the array's current order. They do not search for the smallest key, largest number, earliest date, or latest timestamp. Associative keys and gaps in numeric keys are fine.

PHP example
<?php

declare(strict_types=1);

$labels = [40 => 'Waiting', 7 => 'Processing', 12 => 'Complete'];

echo array_first($labels) . PHP_EOL;
echo array_last($labels) . PHP_EOL;
echo array_key_first($labels) . PHP_EOL;
echo array_key_last($labels) . PHP_EOL;

// Prints:
// Waiting
// Complete
// 40
// 12

Choose array_key_first() or array_key_last() when you need a key. Choose the new helpers when you need the value stored there. An expression such as $labels[0] asks for a specific key, which this array does not contain.

For a list of events, decide what the order means before labelling the last value “most recent”. A list supplied in descending date order would put the most recent event first. Reading an endpoint cannot establish an ordering that the input does not already have.

Handle an empty array

A dashboard might have no notifications yet. Use the null coalescing operator when a missing or null endpoint should produce a display fallback.

PHP example
<?php

declare(strict_types=1);

$notifications = [];

echo (array_last($notifications) ?? 'No notifications yet') . PHP_EOL;
echo var_export(array_first($notifications), true) . PHP_EOL;

// Prints:
// No notifications yet
// NULL

The ?? operator preserves values such as 0, false, and an empty string. Those can be legitimate data. A truthiness check would group them with an absent result, which may give your application the wrong answer.

Distinguish an empty array from a null value

An array can contain null as a real value. If that matters to your application, check whether the array itself is empty before reading its endpoint.

PHP example
<?php

declare(strict_types=1);

$readings = [null];

if ($readings === []) {
    echo 'No readings received' . PHP_EOL;
} else {
    echo 'A reading was received' . PHP_EOL;
    echo var_export(array_first($readings), true) . PHP_EOL;
}

// Prints:
// A reading was received
// NULL

Here the array records that one reading arrived, even though its value is unknown. Replacing the condition with array_first($readings) === null would lose that distinction. Choose the check based on whether your question concerns the container or the value inside it.

Inspect without moving the internal pointer

Older PHP code sometimes uses reset() and end() to read endpoints. Those functions also move the array's internal pointer. The new helpers leave that pointer alone.

PHP example
<?php

declare(strict_types=1);

$steps = ['validate', 'save', 'notify'];
next($steps);

echo array_first($steps) . PHP_EOL;
echo array_last($steps) . PHP_EOL;
echo current($steps) . PHP_EOL;

// Prints:
// validate
// notify
// save

The pointer is still on save after both reads. You rarely need pointer functions in new application code, but recognising this difference helps when maintaining older code. Do not replace a call to end() blindly if later code relies on the pointer having moved.

Supporting PHP versions before 8.5

Check the PHP version used by your application before adopting these functions. Calling them on an older runtime without a polyfill causes an undefined-function error. The feature availability guide explains how to review a project's runtime baseline.

On PHP 7.3 or newer, you can read the endpoint keys and then retrieve the corresponding values. Guard the empty case so that you never use a missing key.

PHP example
<?php

declare(strict_types=1);

$names = ['primary' => 'Ada', 'backup' => 'Grace'];

$first = $names === [] ? null : $names[array_key_first($names)];
$last = $names === [] ? null : $names[array_key_last($names)];

echo $first . PHP_EOL;
echo $last . PHP_EOL;

// Prints:
// Ada
// Grace

Use the direct helpers once your project's supported runtime includes PHP 8.5. Keep endpoint selection separate from decisions about sorting, validating records, and whether an empty collection is acceptable.