Friday, December 12, 2025
HomeLanguagesPHP key_​exists() Function

PHP key_​exists() Function

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

RELATED ARTICLES

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS