1

While researching how to detect null values in an array, I came across some user's comment under the http://www.php.net/manual/en/function.array-key-exists.php manual page.

It said that

if (isset(..) || array_key_exists(...))
{
...
}

is faster than doing

if array_key_exists(...))
{
...
}

The bench marks posted for 100000 runs were

array_key_exists() : 205 ms
is_set() : 35ms
isset() || array_key_exists() : 48ms

My question:

Is (isset(..) || array_key_exists(...)) faster than array_key_exists() ? If so, why?

EDIT: In writing out this question I think I found my answer. I've decided to post the question anyway to see if my thinking is correct.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
TryHarder
  • 2,704
  • 8
  • 47
  • 65
  • 1) Benchmark it (the specific thing you are, you know, asking) under the *actual usage pattern* 2) It [usually] Just Doesn't Matter (so write Good code and not It-Might-Be-Infinitesimal-Faster-Under-Specific-Conditions code). –  Mar 14 '12 at 03:29
  • Benchmark would mean more if we could see the array() used for each iteration. – Mike Purcell Mar 14 '12 at 03:47
  • I didn't run the benchmark myself so I can't provide you with any more information than is at http://www.php.net/manual/en/function.array-key-exists.php Sorry – TryHarder Mar 14 '12 at 03:59

3 Answers3

5

Which is faster depends on the array you are checking. If the array contains a value other than null, "", or 0

if (isset(..) || array_key_exists(...)){    
}

the above code will be faster, because isset will be checked then the code executed. Array_key_exists will not be run.

If the array contains a value of null, "", or 0 then isset will be tested and then array_key_exists. This will take longer than simply testing for array_key_exists by itself.

So the question which is faster depends a lot on the array you are checking.

A lot of people have said that it doesn't really matter. They didn't explain why it doesn't matter though. I guess they mean that the speed improvements are so minimal that it isn't worth bothering with. They may also mean that which is faster is dependent on the values assigned in your array (and thus different each time.)

Ultimately though, if you know that most of the keys will be assigned values other than null, "" or 0 and you really need to determine when null values are assigned, then use

if (isset(..) || array_key_exists(...)){    
}
TryHarder
  • 2,704
  • 8
  • 47
  • 65
0

Sorry, but to detect null values in an array, you can first CHECK the array using in_array which will tell you if the array even contains a "null". I'm not quite sure what you want to actually achieve, do you want to find a null value or what? Will edit when you tell us more.

DanRedux
  • 9,119
  • 6
  • 23
  • 41
  • 1
    The array will definitely contain null values and so I will be looking for which keys contain those null values. This is irrelevant though, because my question asks "is (isset(..) || array_key_exists(...)) faster than array_key_exists(), if yes why?" – TryHarder Mar 14 '12 at 03:56
0

The most reliable method would be isset() || array_ky_exists() as it checks the most, and short-circuit evaluation will speed up the process.

However, as pst said in the comments: It just doesn't matter. Really, it makes almost no difference in the end.

Basically, though, no, it is not. In the end, basic math would tell you that two checks is not faster than one check.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62