According to PHP documentation, the following expressions return true when calling empty($var)
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- $var; (a variable declared, but without a value)
I've found how to "solve" the problem by using empty($var) && $var != 0 but why php developers did it?
I think it is ridiculous, suppose you have this code:
if (empty($_POST["X"])) {
doSomething();
}
I think "0" is not empty, empty is when there is nothing!
Maybe it's better to use
if (isset($x) && x != "") {//for strings
doSomething();
}
emptyis defined like this, since"0"is quite clearly not empty."" == 0 == 0.0 == "0" == null == false == array() == $var. php is loosely typed.