What is the difference between:
isset($foo['bar'])
array_key_exists('bar', $foo)
Is there a performance difference? Is one way more correct than the other?
What is the difference between:
isset($foo['bar'])
array_key_exists('bar', $foo)
Is there a performance difference? Is one way more correct than the other?
The difference is that if bar is null isset()
will return FALSE, while array_key_exists()
will still return TRUE:
$foo['bar'] = NULL;
echo isset($foo['bar']) ? 'true':'false';
echo array_key_exists('bar',$foo) ? 'true':'false';
So it depends on your intentions.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.