What's quicker and better to determine if an array key exists in PHP?
- by alex
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.