What is the best way to define that a value does not exist in PHP, or is not sufficent for the applications needs.
$var = NULL
, $var = array()
, $var = FALSE
?
And what is the best way to test?
isset($var)
, empty($var)
, if($var != NULL)
, if($var)
?
Initializing variables as what they will be, e.g. NULL
if a string, array()
if they will be arrays, has some benefits in that they will function in the setting they are ment to without any unexpected results.
e.g. foreach($emptyArray)
won't complain it just wont output anything, whereas foreach($false)
will complain about the wrong variable type.
But it seams like an unnecessary hassle to have so many different ways of doing basically the same thing. eg. if(empty($var))
or if ($var == NULL)
Duplicate: Best way to test for a variable’s existence in PHP; isset() is clearly broken