What's quicker and better to determine if an array key exists in PHP?

Viewed 137338

Consider these 2 examples...

$key = 'jim';

// example 1
if (isset($array[$key])) {
    // ...
}

// example 2    
if (array_key_exists($key, $array)) {
    // ...
}

I'm interested in knowing if either of these are better. I've always used the first, but have seen a lot of people use the second example on this site.

So, which is better? Faster? Clearer intent?

11 Answers

isset() is faster, but it's not the same as array_key_exists().

array_key_exists() purely checks if the key exists, even if the value is NULL.

Whereas isset() will return false if the key exist and value is NULL.

With Php 7 gives the possibility to use the Null Coalescing Operator.

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

So now you can assign a default value in case the value is null or if the key does not exist :

$var = $array[$key] ?? 'default value'

Well, the main difference is that isset() will not return true for array keys that correspond to a null value, while array_key_exists() does.

Running a small benchmark shows that isset() it's faster but it may not be entirely accurate.

there is a difference from php.net you'll read:

isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does.

A very informal test shows array_key_exists() to be about 2.5 times slower than isset()

As to "faster": Try it (my money is on array_key_exists(), but I can't try it right now).

As to "clearer in the intent": array_key_exists()

Obviously the second example is clearer in intent, there's no question about it. To figure out what example #1 does, you need to be familiar with PHP's variable initialization idiosyncracies - and then you'll find out that it functions differently for null values, and so on.

As to which is faster - I don't intend to speculate - run either in a tight loop a few hundred thousand times on your PHP version and you'll find out :)

I wanted to add that you can also use isset to search an array with unique elements. It is lot faster than using in_array, array_search or array_key_exists. You can just flip the array using array_flip and use isset to check if value exists in the array.

<?php

$numbers = [];
for ($i = 0; $i < 1000000; $i++) {
    $numbers[] = random_int("9000000000", "9999999999");
}

function evaluatePerformance($name, $callback)
{
    global $numbers;
    $timeStart = microtime(true);

    $result = $callback("1234567890", $numbers) ? 'true' : 'false';

    $timeEnd = microtime(true);
    $executionTime =  number_format($timeEnd - $timeStart, 9);

    echo  "{$name} result is {$result} and it took {$executionTime} seconds. <br>";
}

// Took 0.038895845 seconds.
evaluatePerformance("in_array", function ($needle, $haystack) {
    return in_array($needle, $haystack);
});

// Took 0.035454988 seconds.
evaluatePerformance("array_search", function ($needle, $haystack) {
    return array_search($needle, $haystack);
});

$numbers = array_flip($numbers);

// Took 0.000024080 seconds.
evaluatePerformance("array_key_exists", function ($needle, $haystack) {
    return array_key_exists($needle, $haystack);
});

// Took 0.000013113 seconds.
evaluatePerformance("isset", function ($needle, $haystack) {
    return isset($haystack[$needle]);
});
Related