Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

PHP key_​exists() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The key_exists() function is an inbuilt function in PHP that is used to check whether the given key exist in the given array or not. If given key exist in the array then it returns true otherwise returns false. This function is an alias of array_key_exists() function.

Syntax:

bool key_exists(string|int $key, array $array)

Parameters: This function accepts two parameters that are described below:

  • $key: This parameter holds the key that we want to check in the array.
  • $array: This parameter holds the array.

Return Value: This function returns a boolean value either TRUE or FALSE depending on whether the key is present in the array or not respectively.

Example 1:

PHP




<?php
  
$arr = array(5, 10, 15, 20, 25);
  
$key = 3;
  
if(key_exists($key, $arr)) {
    echo "Key Found \n";
}
else {
    echo "Key Not Found \n";
}
  
$arr1 = array(1.1, 2.3, 1.8, 2.07, 1.25);
  
$key1 = 8;
  
if(key_exists($key1, $arr1)) {
    echo "Key Found";
}
else {
    echo "Key Not Found";
}
  
?>


Output:

Key Found 
Key Not Found

Example 2:

PHP




<?php
  
$arr = array(
    "Geeks" => 10,
    "GFG" => 60,
    "G4G" => 80,
    "Geek" => 100
);
  
$key = "GFG";
  
if(key_exists($key, $arr)) {
    echo "GFG Key Found \n";
}
else {
    echo "GFG Key Not Found \n";
}
  
$key1 = "Welcome";
  
if(key_exists($key1, $arr)) {
    echo "Welcome Key Found";
}
else {
    echo "Welcome Key Not Found";
}
  
?>


Output:

GFG Key Found
Welcome Key Not Found

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


My Personal Notes arrow_drop_up
Last Updated : 22 Jul, 2022
Like Article
Save Article
Similar Reads
Related Tutorials