I have encounteed an issue with php treating "0" differently.
I run following script on 2 different machines:
$a = "0";
if ($a) {
echo("helo");
}
1) Local Machine -> PHP 5.2.17 -> it treated "0" as valid and print the 'helo'
2) Server -> PHP 5.3.6 -> it treated "0" as empty/false and won't print the 'helo'
Is this due to the php configuration (if yes, what configuration) or php version?
The following things are considered to be empty: "" (an empty string) 0 (0 as an integer) "0" (0 as a string) NULL.
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .
A variable is considered empty if it does not exist or if its value equals false .
You should use the empty() construct when you are not sure if the variable even exists. If the variable is expected to be set, use if ($var) instead. empty() is the equivalent of ! isset($var) || $var == false .
That's how it is supposed to. PHP interprets strings in boolean context. The "0"
there is equivalent to an actual 0
. (See also http://www.php.net/manual/en/types.comparisons.php)
What you meant to test for is probably:
if (strlen($a)) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With