16

I've seen a lot of people do the former, is there any performance benefit doing one vs the other? Or is it just an eye candy? I personally use the latter every time as it is shorter and personally more readable to me.

Salman A
  • 248,760
  • 80
  • 417
  • 510
Andreas Wong
  • 57,842
  • 19
  • 104
  • 123
  • 7
    Note, both statements are not equal. If $array[$key] is "" or 0, it is empty but the key still exists. – Kaken Bok Jul 30 '11 at 16:58

4 Answers4

16

The other responses focus on the differences between the two functions. This is true, but if the source array does not contain null or 0 or "", ... (empty values) values you can benchmark the speed of the two functions:

<?php

function makeRandomArray( $length ) {
    $array = array();
    for ($i = 0; $i < $length; $i++) {
        $array[$i] = rand(1, $length);
    }

    return $array;
}

function benchmark( $count, $function ) {
    $start = microtime(true);
    for ($i = 0; $i < $count; $i++) {
        $function();
    }
    return microtime(true) - $start;
}

$runs = 100000;
$smallLength = 10;
$small = makeRandomArray($smallLength);

var_dump(benchmark($runs, function() {
    global $small, $smallLength;
    array_key_exists(rand(0, $smallLength), $small);
}));
var_dump(benchmark($runs, function() {
    global $small, $smallLength;
    !empty($small[rand(0, $smallLength)]);
}));

Which gave me the following results:

For a small array:

  • array_key_exists: float(0.18357992172241)
  • empty: float(0.072798013687134)
  • isset: float(0.070242881774902)

For a relative big array:

  • array_key_exists: float(0.57489585876465)
  • empty: float(0.0068421363830566)
  • isset: float(0.0069410800933838)

So if it's possible it's faster to use empty or isset.

KARASZI István
  • 29,989
  • 8
  • 97
  • 120
  • 2
    try running the same test on `isset()`. – Salman A Jul 30 '11 at 17:16
  • interesting stats, thanks. I thought `isset()` was the fastest but seems like its performance is equivalent to that of empty. – Salman A Aug 01 '11 at 05:25
  • `array_key_exists`, `isset` and `empty` behaves in a very different way. It shouldn't be a matter of performance only – Yuri May 16 '17 at 06:54
  • Such performance difference is because isset() and empty() are language constructs while array_key_exists() is regular built-in function. – xZero Jul 20 '18 at 13:43
9

array_key_exists($key, $array) and !empty($array[$key]) can produce different results therefore it is not a matter of performance or preference.

                              | array_key_exists($key, $array) | !empty($array[$key]) |
+-----------------------------+--------------------------------|----------------------+
| $array[$key] does not exist | false                          | false                |
| $array[$key] is truthy      | true                           | true                 |
| $array[$key] is falsey      | true                           | false                |

You can see that the truth table is different for falsey values (false, 0, NULL, etc). Therefore !empty($array[$key]) is not suitable in situations where a falsey value could be considered present e.g. $array["number_of_children"] should not be tested for emptiness where the value 0 makes sense.


You can use isset($array[$key]) which produces results identical to array_key_exists($key, $array) with exactly one exception:

                                      | array_key_exists($key, $array) | isset($array[$key]) |
+-------------------------------------+--------------------------------|---------------------+
| $array[$key] does not exist         | false                          | false               |
| $array[$key] is truthy              | true                           | true                |
| $array[$key] is falsey but not NULL | true                           | true                |
| $array[$key] is NULL                | true                           | false               |
Salman A
  • 248,760
  • 80
  • 417
  • 510
4
$array = array(
    'foo' => null
);

echo (int)!empty($array['foo']); // 0
echo (int)array_key_exists('foo', $array); // 1
Yoshi
  • 53,111
  • 13
  • 85
  • 102
1

They both are different

array_key_exists($key, $array) checks whether the key exist in the array and returns TRUE if the given key is set in the array.

whereas

!empty($array[$key]) Determine whether a variable value is empty or not

Rahul
  • 73,987
  • 13
  • 62
  • 116