Reputation: 25
if ("false" == 0) echo "true\n";
// => true
While PHP documentation says:
var_dump((bool) "false"); // bool(true)
If it is a bug where can I file it?
Upvotes: 0
Views: 150
Reputation: 1751
This might help: These are the only things that PHP considers false:
Try to avoid string comparisons of text and stick with booleans!
Upvotes: 0
Reputation: 2119
The problem is that PHP casts variables unless you use a strict comparison. As others mentioned, the string "false"
is being cast to an integer.
If you want to do a comparison without typecasting use:
"false" === 0
Upvotes: 0
Reputation: 212412
It is not a bug, it's a well documented feature when comparing values of different datatypes - in this case, string to integer. PHP converts the string to an integer to do the comparison, and "false" converts to 0.
See the manual:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
and this page on string to number conversion
If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
To avoid this behaviour, use comparison with strict typing:
if ("false" === 0) echo "true\n";
Upvotes: 5
Reputation: 723598
Nope, not a bug. Don't bother filing it.
This seemingly inconsistent behavior has nothing to do with the word "false". It has to do with your string consisting of only letters and no numeric digits, so when it gets cast to an integer for comparison (== 0
), it results in 0, making 0 == 0
which evaluates to true.
If you compare any other alphabetic string to 0, you'll get the same result:
"abcd" == 0
"a" == 0
"true" == 0
Upvotes: 7