Saturday, December 28, 2024
Google search engine
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

Recent Comments