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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_first(array $array): mixed

Parameters

ParameterDescription
array The input array.

Return value

The first value, or null.

Examples

First value
<?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.

Order, not key
<?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

VersionChange
PHP 8.5Added

In other languages

Was this page helpful?