i used this way to testing an array $arr if empty
if(!empty($arr)){ //do something }
recently, i saw someone use an another like:
if($arr){ //do something }
just want to know is’t the second way a simply way to testing an array or is there some potential risk here ?
An empty array like
array()is regarded as equal tofalse. So a simpleif ($arr)works perfectly fine.emptydoes the same kind of comparison, but does not trigger aNOTICEabout missing variables, should the variable$arrnot exist at all. You should not useemptyif you are sure the variable exists, since it suppresses valuable error reporting. Only useemptyif you really don’t know whether a variable exists or not and have no control over it.For more information about
emptysee The Definitive Guide To PHP’s isset And empty.