Archivo de Agosto de 2008

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?



Sobre mi

  • Qué hago: Qué rica estaba el agua contaminada, pero creo que este año no tomaré más. Bilardo ya se la jugó así a los brasileños. 2 hrs ago

Suscríbete al RSS

Si quieres ser avisado cuando escribo algo nuevo, simplemente tienes que añadir el feed a tu lector favorito, o introducir tu email y te enviaré un correo.

Categorías