Programming (aka scripting/writing) PHP is so funny you never know what is the next thing that will happen when you compare two variables or simply try to increment an integer.

You can complain about its design (OOP), or its functions’ names or whatever you want, but you know how cool and funny is to program in PHP. It should be a must within any programming 101 course ;-).

Here my compilation of PHP funniest things:

printf("Integers and Strings\n");
echo "----------------\n";
printf("%s\n", ('0' == 0) ? 'true' : 'false');
printf("%s\n", ('0' === 0) ? 'true' : 'false');
printf("%s\n", ('string' == 0) ? 'true' : 'false');
printf("%s\n", ('string' === 0) ? 'true' : 'false');
printf("%s\n", ('string' == 1) ? 'true' : 'false');
printf("%s\n", ('string' === 1) ? 'true' : 'false');
printf("%s\n", ('0000e12345' == '0e12345') ? 'true' : 'false');
printf("%s\n", ((string) '0123' == (string) '123') ? 'true' : 'false');
printf("%s\n", ('007' == '7') ? 'true' : 'false');
printf("%s\n", ('Bond' == 'Raul') ? 'true' : 'false');
$i = null;
echo (++$i) . "\n";
echo ($i+=1) . "\n";
$i = null;
echo (--$i) . "\n";
echo ($i-=1) . "\n";

printf("\nBitwise Operators\n");
echo "----------------\n";
printf("%s\n", (~0 & 0x20) ? 'true' : 'false');
printf("%s\n", (~'0' & 0x20) ? 'true' : 'false');
echo (9 & 8) . "\n";
printf("%s\n", (9 & 8 == 8) ? 'true' : 'false');
echo (8 & 8) . "\n";
printf("%s\n", (8 & 8 == 8) ? 'true' : 'false');

echo "\nIEEE 754 friends et al\n";
echo "----------------\n";
echo (7 % 2.4) . "\n";
printf("%s\n", -1 % 7);
printf("%s\n", 3333333333 % 3);
printf("%s\n", 2863311530 % 25);
printf("%s\n", 2863311530 % 256);

printf("\nOther comparisons\n");
echo "----------------\n";
printf("%s\n", ($a = 0 or $a = 'string') ? $a : $a);
printf("%s\n", ($a = 0 || $a = 'string') ? $a : $a);
printf("%s\n", (array() == false) ? 'true' : 'false');
printf("%s\n", (array() === false) ? 'true' : 'false');

It should produce 29 results. It’s said that one guy once was able to predict (Yeah! I meant to predict) the exact output for every single one. PHP is so lovely.

Could you say what is the exact output for each and everyone of them?



About Raúl

Raúl Ochoa, a spaniard working for Tuenti in Madrid, Spain. More about me.

Subscribe to the feed

If you want to receive a notification when I update the website, you only have to add the feed to your reader, or submit your email address and I'll let you know.

Twitter

Raúl's Twitter: Non-tweetable RegEx(s) make me cry :'-(. 3 days ago

Categories