array_key_exists($name, $defaults)
isset($defaults[$name])
views:
61answers:
2
+1
A:
Here is a quick comment from the PHP manual talking about the performance differences between the two! But they do the same thing :-\
Strike that, I'm an idiot.
Urda
2010-03-03 17:58:07
No, they don't do the same thing : `isset` will return `false` when a value is defined, but `null` ; `array_key_exists`, on the other hand, will return `true` ;;; see the note on Example #2 : http://php.net/array_key_exists
Pascal MARTIN
2010-03-03 18:02:44
+5
A:
Yes, there is a difference. isset
returns false if the value is null while array_key_exists
doesn’t:
$defaults = array('foobar' => null);
var_dump(array_key_exists('foobar', $defaults)); // bool(true)
var_dump(isset($defaults['foobar'])); // bool(false)
So you should always use array_key_exists
for array keys unless you don’t want to make a difference whether an array item exists or is null.
Gumbo
2010-03-03 18:01:20