Wednesday, May 6, 2015

PHP Blunder-2

Here is another 'oops!' moment for PHP. Do you remember the rule of transitivity? A related to B, and A related to C, implies that B is related to C. Right? Now apply this very understanding to the code below.
<?php
echo ("foo" == 0)?"foo equals 0\n":"foo does not equal 0\n"; // prints "foo equals 0"
echo ("foo" == true)?"foo equals true\n":"foo does not equal true\n"; // prints "foo equals true"
echo (true == 0)?"true equals 0\n":"true does not equal 0\n"; // prints "true does not equal 0"
?>
view raw awkward-php.php hosted with ❤ by GitHub
Now, foo equals true, and foo equals zero. So, according to this rule of transitivity, true equals zero?!

No comments:

Post a Comment