tags:

views:

61

answers:

2
array_key_exists($name, $defaults)

isset($defaults[$name])
+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
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
+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