array_first() and array_last()
PHP 8.5 added the two most obviously-missing array functions. Get the first or last value of an array without reset() side effects, without array_key_first() + indexing, and without any of the other workarounds developers have been using for 25 years.
$events = ['signup', 'login', 'purchase', 'logout'];
$first = array_first($events); // 'signup'
$last = array_last($events); // 'logout'
// Returns null for empty arrays — compose with ??
$latest = array_last($logs) ?? 'No logs yet';
// Works with associative arrays too
$config = ['debug' => true, 'env' => 'prod', 'version' => '3.0'];
array_first($config); // true (first value)
array_last($config); // '3.0' (last value)
// Compare with the old ways:
$first = reset($array); // Mutates internal pointer!
$first = $array[array_key_first($array)]; // Verbose
$first = current(array_slice($array, 0, 1)); // Allocates a new array
$last = $array[array_key_last($array) ?? 0] ?? null; // Awkward null handling
The old approaches either mutated the array's internal pointer (reset()/end()), required two function calls, or created unnecessary intermediate arrays. array_first() and array_last() do exactly one thing, correctly, with no side effects.
Significance: Completeness
PHP had
array_key_first() and array_key_last() since 7.3 but inexplicably not the value equivalents. It took six more years, but the standard library is finally complete for this basic operation. Sometimes the best features are the ones that should have existed all along.