mamdouh asked this 2 years ago

PHP How to check if a key exists in an associative array?

Should i use isset() or array_key_exists()?


pseudoFish 2 years ago
2 likes

$arr["foo"] = bar;

You can use both

if(isset($arr["foo"]))
{//do something}

or

if(array_key_exists("foo",$arr))
{// do something}

But if 

$arr["foo"] = null;

isset($arr["foo"])
returns FALSE

array_key_exits("foo",$arr)
returns TRUE

So it depends on what action you want to take if value is NULL at that key.