What's quicker and better to determine if an array key exists in PHP?
Posted
by alex
on Stack Overflow
See other posts from Stack Overflow
or by alex
Published on 2009-03-31T06:17:31Z
Indexed on
2010/05/18
23:20 UTC
Read the original article
Hit count: 209
php
|best-practices
Consider these 2 examples
$key = 'jim';
// example 1
if (isset($array[$key])) {
doWhatIWant();
}
// example 2
if (array_key_exists($key, $array)) {
doWhatIWant();
}
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?
Update
Thanks for the quality answers. I now understand the difference between the 2. A benchmark states that isset()
alone is quicker than array_key_exists()
. However, if you want the isset()
to behave like array_key_exists()
it is slower.
© Stack Overflow or respective owner