On Friday, Alin D. has given our team the following PHP problem to solve:
if (SOMETHING) {
echo ‘Hello’;
} else {
echo ‘ world’;
}
He asked our colleagues to replace SOMETHING, so that the outcome would be Hello World.
At the end of the day, Alin showed the solutions he received:
1. if (!print(‘Hello’)) { (most of our colleagues thought of this solution)
2. if (((print(‘ Hello’)) && 0)) {
3. printf(“Hello “)==”4”
4. !printf(“Hello”)
There were several very clever solutions, such as:
5. php -r ‘if (eval(“echo “Hello”;”)){ echo “Hello”;} else {echo ” world”;}’
6. if(($a = function(){echo ‘hello ‘; return false;}) && $a() === true)
7. if (die(‘Hello world’)) {
8. if (printf(‘Hello world<div style=”display:none”>’)) { (that doesn’t work in CLI or IE)
9. php -r “if(print(“Hello worldr”)){echo’Hello’;}else{echo’ world’;} echo PHP_EOL;”
10. php -r “if(print(‘Hello world’)){} __halt_compiler(); ){echo’Hello’;}else{echo’ world’;}”
11. if(eval(“echo ‘hello ‘; return false;”))
Needless to say, these solutions were very cool – and we were really impressed by the large number of solutions they found!
How would you have solved this problem?