PHP key_exists() Function

PHP key_exists() Function

The key_exists() function in PHP checks if the specified key or index exists in the array or not.

Syntax

key_exists ( mixed $key , array $array ) 

Parameter

key(required)- This parameter represents the value to check.

array(required)- This parameter signifies an input array with keys to check.

Return

This function returns a Boolean value TRUE on success or FALSE on failure.

Example 1

'first', 2=>'second',3 => 'third',4=>'fourth',5=>'fifth');
 // passing the key value
 $key= 3;
 // will check whether the key exists or not
 if (key_exists($key, $search_array)) { 
     echo "The $key key element is present in the array";
 }
 else {
     echo "The $key key element is not present in the array";
 }
 ?> 

Output

The 3 key element is present in the array

Example 2

'first', 2=>'second',3 => null, 4=>'fourth');
 //printing the input array
 print_r($search_array);
 // passing the key value
 $key= 3; 
 // will check whether the key exists or not
 if (key_exists($key, $search_array)) {
     echo "The $key key element is present in the array";
 }
 else {
     echo "The $key key element is not present in the array";
 }
 ?> 

Output

The 3 key element is present in the array

Example 3

 

Output

The 3 key element is not present in the array