What's quicker and better to determine if an array key exists in PHP?
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
.
If you are interested in some tests I've done recently:
https://stackoverflow.com/a/21759158/520857
Summary:
| Method Name | Run time | Difference
=========================================================================================
| NonExistant::noCheckingTest() | 0.86004090309143 | +18491.315775911%
| NonExistant::emptyTest() | 0.0046701431274414 | +0.95346080503016%
| NonExistant::isnullTest() | 0.88424181938171 | +19014.461681183%
| NonExistant::issetTest() | 0.0046260356903076 | Fastest
| NonExistant::arrayKeyExistsTest() | 1.9001779556274 | +209.73055713%
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.