PHP 8 reference · Array Functions

PHP array_key_exists() Function

Check whether an array has a given key, even if its value is null. isset() returns false for null values.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_key_exists(string|int|float|bool|resource|null $key, array $array): bool

Parameters

ParameterDescription
key The key to look for.
array The input array.

Return value

true if the key exists.

Examples

array_key_exists() vs isset()Try it yourself
<?php
$settings = ["theme" => "dark", "beta" => null];
var_dump(array_key_exists("beta", $settings));
var_dump(isset($settings["beta"]));

Output

bool(true)
bool(false)
Read with a fallbackTry it yourself
<?php
$row = ["id" => 7];
echo array_key_exists("email", $row) ? $row["email"] : "no email", "\n";
echo $row["email"] ?? "no email", "\n"; // shorter when null means missing

Output

no email
no email

key_exists() is an alias. For speed and brevity, isset($a[$k]) is fine when a null value can be treated as missing.

Version history

VersionChange
PHP 4.0Added
PHP 8.0No longer works on objects; use property_exists() or isset().

In other languages

Was this page helpful?