The empty() native in PHP will fail if the string is "0" or something like that.
So how to implement the exact is_empty() function in PHP?
The empty() native in PHP will fail if the string is "0" or something like that.
So how to implement the exact is_empty() function in PHP?
Quite simple, check for the error case described above. If you run into it, return the result you want. If not, call empty() and return that result.
try this:
function is_empty(&$val) {
return empty($val) && $val !== "0";
}
&$val
is needed, so you don’t get a warning on undefined variables.
if you only want to check if a variable is set (regardless of its value) you should use PHP’s isset
You may want to use isset if you are trying to check if that variable is defined.
Check out the type comparison table in the PHP manual for the exact behaviour of empty(), isset(), is_null() etc. You'll probably find what you're looking for there.