Wednesday, July 29, 2026
HomeLanguagesPHP array_​key_​first() Function

PHP array_​key_​first() Function

The array_​key_​first() function is an inbuilt function in PHP that is used to get the first key of an array. This function returns the first key without affecting the internal array pointer.

Syntax:

int|string|null array_key_first(array $array)

Parameters: This function accepts a single parameter $array that holds the array.

Return Value: This function returns the first key of the array if the array is not empty, and null otherwise.

Example 1:

PHP




<?php
  
$arr = array();
  
var_dump(array_key_first($arr));
  
$arr1 = array("10", "20", "30", "40", "50");
var_dump(array_key_first($arr1));
  
?>


Output:

NULL
int(0)

Example 2:

PHP




<?php
  
$arr = array(
    'Geeks'  => "HTML"
    'GFG'  => "CSS"
    'Geek'  => "JavaScript"
    'G4G' => "PHP"
);
  
var_dump(array_key_first($arr));
  
$arr1 = array(
    5 => "neveropen",
    10 => "GFG"
);
  
var_dump(array_key_first($arr1));
  
?>


Output:

string(5) "Geeks"
int(5)

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

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS