Hi guys,
Today, I am very confused because PHP accepts the below condition.
<?php
$b = true;
if($b == 'hello') echo 'ok'; else echo 'no';
?>
Well, PHP displays ‘ok’. I still don’t understand how is it possible.
Maybe, you can clarify it for me…
It’s because doing a comparison using double equals (==) does type conversion to compare the two values. In your example, the string ‘hello’ is evaluated to true.
If you use triple equals (===) PHP won’t try to convert the types, and so your script will output ‘no’.
1 Like
Yes, in some cases type conversion might be a good thing, and many times it won’t make a difference, but for the most part it I find it best to use stricter comparison. Something I’ve learned the hard way over time.