the sneaky difference between isset and arraykeyexists in php

The Sneaky Difference Between isset() and array_key_exists() in PHP

On the surface, isset() and array_key_exists() seem to do the exact same thing. Yet, there’s a situation where they don’t. Let’s take a look at the difference between the two.

While isset() and array_key_exists() can both be used to check if a key exists in an array, and will return the same result in most cases, isset() will return false if the value of the key is null, while array_key_exists() will return true.

Check out the example below:

$empty_array = [];

$test_array = [
    'key1' => 'value1',
];

$array_with_empty_string = [
    'key1' => '',
];

$array_with_null_value = [
    'key1' => null,
];

// Test 1: isset() vs array_key_exists() with empty array
var_dump(isset($empty_array['key1'])); // false
var_dump(array_key_exists('key1', $empty_array)); // false

// Test 2: isset() vs array_key_exists() with array with key
var_dump(isset($test_array['key1'])); // true
var_dump(array_key_exists('key1', $test_array)); // true

// Test 3: isset() vs array_key_exists() with array with empty string
var_dump(isset($array_with_empty_string['key1'])); // true
var_dump(array_key_exists('key1', $array_with_empty_string)); // true

// Test 4: isset() vs array_key_exists() with array with null value
var_dump(isset($array_with_null_value['key1'])); // false
var_dump(array_key_exists('key1', $array_with_null_value)); // true

As you can see, the only difference between isset() and array_key_exists() occurs when evaluating the array with a null value.

Evaluating an empty array, an array with or without a key, or an array with an empty string will return the same result for both isset() and array_key_exists().

Which Is Faster: isset() or array_key_exists()?

Let’s check out the performance of both functions, by looping over an array 1,000,000 times and checking if a key exists in the array.

$test_array = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
    'key4' => 'value4',
    'key5' => 'value5',
];

# Test 1: isset() 1,000,000 times on array with 5 keys
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
    isset($test_array['key1']);
}
$end = microtime(true);
echo 'isset() took ' . ($end - $start) . ' seconds' . PHP_EOL;

# Test 2: array_key_exists() 1,000,000 times on array with 5 keys
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
    array_key_exists('key1', $test_array);
}
$end = microtime(true);
echo 'array_key_exists() took ' . ($end - $start) . ' seconds' . PHP_EOL;

>>> isset() took 0.055624961853027 seconds
>>> array_key_exists() took 0.060150146484375 seconds

isset() is 1.08x faster than array_key_exists() because it is a language construct and array_key_exists() is a function. isset() is also more readable than array_key_exists(). Yet, the performance gain is not huge. So unless you need to check if a key exists in an array with null being a valid value, you can use either function.