Pigs Can Fly

Php is so loosely typed that true==false in php.

$a = ‘foo’;
$b = 0;
if ( $a == true && $b == false && $a == $b ) {
echo “Pigs Can Fly”;
}

Produces “Pigs Can Fly”.
Strings are considered true if they are not empty or null. so ‘foo’==true would be true==true ie true
Numbers are considered false if they are 0. so 0 == false is false == false ie true
As for the last point this still fascinates me.
Because there are no numbers in variable $a, is set to 0, therefore int 0 == int 0.

Source

Tags:

Wednesday, November 12th, 2008 Programing