PHP 8 reference · Array Functions
PHP array_first() Function
Return the first value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.
PHP 8 Since 8.5
Syntax
array_first(array $array): mixed
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
Return value
The first value, or null.
Examples
<?php
$scores = ["ann" => 90, "bob" => 72];
echo array_first($scores), "\n";
var_dump(array_first([]));
Output
90 NULL
Run this on your computer — see “Get Started” for setup.
<?php
$list = [3 => "c", 1 => "a"];
echo array_first($list), "\n"; // insertion order, not the smallest key
Output
c
Run this on your computer — see “Get Started” for setup.
Before PHP 8.5, use $array[array_key_first($array)] ?? null, or reset($array) (which moves the internal
pointer and returns false for an empty array).
Version history
| Version | Change |
|---|---|
| PHP 8.5 | Added |
In other languages
array.at()· JavaScript
Related
array_last()· Return the last value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.array_key_first()· Return the first key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.- PHP Arrays · tutorial
- PHP Array Functions · tutorial