if ("blah"==0) {echo "true";} else {echo "false";}
This is returning true. Why? How is “blah” in any way like 0?
if ("blah"==0) {echo "true";} else {echo "false";}
This is returning true. Why? How is “blah” in any way like 0?
Then use ‘===’ instead
if ("blah" === 0) {echo "true";} else {echo "false";}
Nope, still don’t understand how “blah” is 0 in any way.
Edit: hang on, blah gets converted to an integer before being compared to 0? That’s the stupidest thing I’ve ever seen. If I’m comparing two things, I’m interested in those two things, I don’t want an apple to be covered with orange peel before being compared to an orange!
The ‘Type Juggling’ manual page maybe of interest.
<?php
var_dump(
(int)'blah'
);
/*
int(0)
*/
?>
If you take ‘blah’ and try to convert it to an integer for the comparison, it will convert to 0 (there are no numerics that can be pulled out). This is why the test works.
I guess what you should be trying to do is ‘blah’ == ‘’ (an empty string), that way no conversion will happen.
As has already been pointed out, if you use === then it doesn’t do the same conversion (read it as ‘absolutely equal’ rather than just ‘equal’).
‘===’ works fine as well
or use something similar to this when comparing with integers
$var = "blah";
if ((int)$var) {echo "true";} else {echo "false";}
This is what a loosely typed language does, it will covert one type to another either explicitly or implicitly. While it may seem “the stupidest thing…” if you come from a language that is strong typed, but that is just how it works. If you need a strict comparison, you would use “===” which compares type and value.