is there any difference between if(!$array) and if(empty($array))

时间:2016-04-04 17:38:30

标签: php arrays if-statement

Is there a case where doing this?

if(!$array) {
//... do stuff
}

Would produce different results than this?

if(empty($array)){
//... do stuff
}

1 个答案:

答案 0 :(得分:0)

Not exactly.

As I found out on the PHP docs, empty() is actually equivalent to !isset($var) || $var == false.

In other words, if(!$array) and if(empty($array) would return the same value unless the $array is not set, in which the first piece of code would return an Exception