PHP 8.5 array_first and array_last: the null value trap
A dashboard wants the most recent event in an array. The convenient last-value helper works, but "there was no event" and "the last event has a null payload" are different states. Losing that distinction can create a misleading empty screen.
Start with insertion order
<?php
$events = [90 => 'import started', 4 => 'import finished'];
assert(array_first($events) === 'import started');
assert(array_last($events) === 'import finished');
echo array_last($events), PHP_EOL;
Expected output: import finished. The greatest numeric key is 90, but it is not the last entry. In this example order was established by the importer, not by sorting identifiers.
Preserve presence separately
<?php
$events = ['last-response' => null];
$hasEvent = $events !== [];
$lastPayload = array_last($events);
assert($hasEvent === true);
assert($lastPayload === null);
assert(array_last([]) === null);
echo $hasEvent ? 'event exists' : 'no events';
The array itself tells us whether anything exists. The helper tells us the value. For a user interface, those can become separate branches: no activity, activity without a payload, and activity with a payload.
Choose the fallback deliberately
A null-coalescing fallback is appropriate when both empty and null should display the same label. It is inappropriate when null means "the upstream system intentionally cleared this field." Write that requirement down before simplifying the expression.
If your application still supports PHP 8.4, use an explicit empty-array check plus array_key_last instead, or an established compatible polyfill. Do not silently raise the production PHP requirement because one development machine has a newer binary.
Test empty input, a single null, a single zero, and mixed numeric keys. These four cases catch more useful behavior than a test containing only three ordinary strings. PHP's array_last reference specifies the empty-array result.