PHP array_key_exists() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_key_exists() checks whether a given key exists in an array.

It returns true even when the value stored under that key is null, which makes it different from isset().

Syntax

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

PHP
$found = array_key_exists("name", $user);
CallWhat it does
array_key_exists("name", $user)Returns true if $user has a name key.
array_key_exists(2, $list)Returns true if $list has an item at index 2.
array_key_exists("age", ["age" => null])Returns true even though the value is null.

Use isset() when you also want to treat null as missing. Use array_key_exists() when you only care that the key is there.

Library declarations
PHP
array_key_exists(string|int $key, array $array): bool

Parameters

ParameterDescription
keyRequired. The key to look for. It can be a string or an integer.
arrayRequired. The array to search in.

Return Value

Return ValueDescription
booltrue if the key exists in the array, otherwise false.

PHP Examples

Example 1 — Check If a Key Exists

Look for keys in a small user array.

PHP
<?php
$user = ["name" => "Ana", "age" => 25];

echo array_key_exists("name", $user) ? "yes" : "no", PHP_EOL;
echo array_key_exists("email", $user) ? "yes" : "no", PHP_EOL;

How It Works

  1. Create an array. It has the keys name and age.
  2. Search for name. The key exists, so the function returns true.
  3. Search for email. There is no such key, so it returns false.
  4. Print a word. The ternary turns the result into yes or no.

Example 2 — Null Values and isset()

A key with a null value still exists, but isset() says no.

PHP
<?php
$data = ["age" => null];

echo array_key_exists("age", $data) ? "exists" : "missing", PHP_EOL;
echo isset($data["age"]) ? "set" : "not set", PHP_EOL;
echo array_key_exists("city", $data) ? "exists" : "missing", PHP_EOL;

How It Works

  1. Store null on purpose. The key age is present, but its value is empty.
  2. Use array_key_exists(). It only checks the key, so it prints exists.
  3. Use isset(). It treats null as not set, so it prints not set.
  4. Check a missing key. city is not in the array at all.

Example 3 — Numeric and String Keys

PHP converts whole-number strings to integer keys, so they match each other.

PHP
<?php
$items = [1 => "x"];

echo array_key_exists(1, $items) ? "yes" : "no", PHP_EOL;
echo array_key_exists("1", $items) ? "yes" : "no", PHP_EOL;
echo array_key_exists("01", $items) ? "yes" : "no", PHP_EOL;

How It Works

  1. Create an array with integer key 1. Its value is x.
  2. Search with the integer 1. It matches.
  3. Search with the string "1". PHP treats it as the integer 1, so it matches too.
  4. Search with "01". That is a different string key, so it does not match.

Frequently Asked Questions

It checks if a key exists in an array and returns true or false. It does not look at the values.
array_key_exists() returns true when the key exists even if the value is null. isset() returns false for null values.
Use in_array() to search values. array_key_exists() only searches keys.
No. Since PHP 8.0 it only accepts arrays. For objects use property_exists().
Yes. array_key_exists(2, $list) checks if index 2 exists. Whole-number strings like "2" are treated as integers.

Next: array_key_first()

array_key_first() returns the first key of an array without changing the array.

array_key_first() 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