Understanding the PHP Function "array_key_exists"
The PHP function array_key_exists is a built-in function in PHP used to check if a specified key exists in an array. This function returns a boolean value of
array_key_exists() is a built-in PHP function that checks whether a given key exists in an array. It returns true when the key is present and false when it is not — regardless of what value is stored under that key. This page explains how the function works, how it differs from isset() (the most common source of bugs), and when you should reach for each one.
Syntax
array_key_exists(string|int $key, array $array): bool$key— the key to look for. Array keys in PHP are always integers or strings, so$keymust be one of those types.$array— the array to search.- Return value —
trueif$keyis a key of$array, otherwisefalse.
Note that it checks the key, not the value. To search for a value instead, use in_array() or array_search().
Basic example
array_key_exists() is most useful with associative arrays, where keys are named rather than numbered.
The output is:
The key 'first_name' exists in the array.It works with integer keys too — including the automatic numeric indexes of a plain list:
<?php
$colors = ['red', 'green', 'blue']; // keys are 0, 1, 2
var_dump(array_key_exists(2, $colors)); // bool(true)
var_dump(array_key_exists(3, $colors)); // bool(false)Output:
bool(true)
bool(false)array_key_exists() vs. isset()
This is the most important thing to understand about the function. Both can test for a key, but they answer different questions:
array_key_exists($key, $array)returnstrueif the key is defined even when its value isnull.isset($array[$key])returnsfalsewhen the value isnull, becauseisset()checks that a variable exists and is not null.
<?php
$data = ['name' => null];
var_dump(array_key_exists('name', $data)); // bool(true) — the key is there
var_dump(isset($data['name'])); // bool(false) — value is nullOutput:
bool(true)
bool(false)Use array_key_exists() when a null value is meaningful (e.g. "the field was submitted but left blank"). Use isset() when you only care whether a usable, non-null value is present — it is also slightly faster for the common case.
When to use it
- Guarding against "undefined array key" warnings before reading a key that might be missing.
- Validating that required keys exist in configuration or form input.
- Distinguishing a key set to
nullfrom a key that is genuinely absent.
Common gotchas
- It is case-sensitive for string keys.
'Name'and'name'are different keys. - It does not search nested arrays. It only checks the top level of the array you pass.
- It does not look at values. Searching for
'John'as a key in['first_name' => 'John']returnsfalse. Usein_array()to search by value. - To list all keys at once, see
array_keys().
Conclusion
array_key_exists() is a small but essential tool for working safely with PHP arrays. Reach for it when you need to know whether a key is defined — and remember that, unlike isset(), it treats a key holding null as existing.