PHP array_first() Function

What is PHP array_first() Function?

If you want to get the first value of an array, use array_first() function.

Syntax:

array_first(array)

Parameters:

The Function has 1 required parameter-

array (Required): The input array whose first value you want to get.

Return Values:

The function returns-

  • The first element of the array
  • If the input array is empty, the function returns NULL.

Examples:

Example 1:

<?php
$language = ["PHP", "Python", "Java", "HTML", "CSS"];
$first_value = array_first($language);
echo "The first value of the array is: " . $first_value;
?>

Output:

The first value of the array is: PHP

Example 2:

<pre>
<?php
$language = [];
$first_value = array_first($language);
echo "The first value of the array is: ";
var_dump($first_value);
?>
</pre>

Output:

The first value of the array is: NULL

PHP Version Support:

PHP 8 >= 8.5.0

Summary: PHP array_first() Function

The array_first() function is a built-in PHP function and part of the PHP’s array functions. It is a useful function to get the first value of the array.

Reference:

https://www.php.net/manual/en/function.array-first.php