PHP key_exists() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, key_exists() checks whether a given key exists in an array and returns true or false. It is an alias of array_key_exists().

Because it is an alias, key_exists() works exactly the same as array_key_exists(). Most code and documentation use the name array_key_exists(), which is clearer, so prefer that name in new code.

Syntax

Call key_exists() with the values you need. Start with this line.

PHP
$has = key_exists("name", $user);
CallWhat it does
key_exists("name", $user)Returns true if the key "name" exists in $user.
key_exists(1, $items)Checks a numeric key, such as the second item of a list.
array_key_exists("name", $user)The main name for the same function. It gives the same result.

key_exists() is an alias of array_key_exists(). It has the same arguments and returns the same result. The key comes first and the array second.

Library declarations
PHP
key_exists(string|int|float|bool|resource|null $key, array $array): bool

Parameters

ParameterDescription
keyRequired. The key to look for. Numeric strings such as "1" are treated as the integer 1.
arrayRequired. The array to search in.

Return Value

Return ValueDescription
booltrue if the key exists in the array, even if its value is null. Otherwise false.

PHP Examples

Example 1 — Check If a Key Exists

This example checks one key that is in the array and one that is not.

PHP
<?php
$user = ["name" => "Asha", "email" => null];

var_dump(key_exists("name", $user));
var_dump(key_exists("age", $user));

How It Works

  1. $user has the keys name and email.
  2. "name" is one of the keys, so key_exists() returns true.
  3. "age" is not a key in the array, so the result is false.
  4. var_dump() prints both booleans.

Example 2 — key_exists() Versus isset() with null

A key can exist while its value is null. key_exists() sees the key and isset() does not.

PHP
<?php
$user = ["name" => "Asha", "email" => null];

var_dump(key_exists("email", $user));
var_dump(isset($user["email"]));

How It Works

  1. The key "email" exists, but its value is null.
  2. key_exists() only asks whether the key is present, so it returns true.
  3. isset() also requires the value to be not null.
  4. So isset() returns false for the same key.

Example 3 — Numeric Keys and Numeric Strings

Lists have numeric keys starting at 0. key_exists() works with them, and a numeric string is treated as a number.

PHP
<?php
$items = ["a", "b"];

var_dump(key_exists(1, $items));
var_dump(key_exists(5, $items));
var_dump(key_exists("1", $items));

How It Works

  1. $items has the keys 0 and 1.
  2. The key 1 exists, so the first result is true.
  3. The key 5 does not exist, so the second result is false.
  4. The string "1" is converted to the integer 1, so the last result is true.

Frequently Asked Questions

Yes. key_exists() is an alias of array_key_exists(), so both behave in exactly the same way.
Use array_key_exists(). It is the main name, appears in nearly all tutorials and makes your code easier for other developers to read.
key_exists() returns true when the key is present, even if its value is null. isset() returns false for null values.
Not directly. It only checks the top level of the array. For a nested key, check each level in turn, or use isset($a["x"]["y"]).
No. Since PHP 8.0 it only accepts arrays. For objects use property_exists() instead.

Next: krsort()

krsort() sorts an array by its keys in reverse (descending) order and keeps the key and va.

krsort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful