Our blog

 

PHP Basics, some thing that is false and true at the same time!

What can be true and false at the same time?

PHP:
  1. $a = "a";
  2. $b = 0;
  3.  
  4. if($a == true && $b == false && $a == $b) {
  5.     echo "Passed";
  6. }

This is because PHP tries to convert "a" to a numeric, as there is no numeric value it gets changed to zero. So 0 == 0 is true. At the same time the string "a" converted to boolean evaluates to true. 0 evaluates to false. While there isn't an actual value that is both true and false that the same type, depending on the context a value may evaluate to true or false depending on what it is been compared to. To avoid problems like this, you should use triple equals (===) unless you know other wise.

More Reading:

 

Leave a Reply