PHP Challenge

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?

10 thoughts on “PHP Challenge”

  1. if (print_r(‘Hello’) === FALSE) {
    echo ‘Hello’;
    } else {
    echo ‘ world’;
    }

    However, since the output must be “Hello World” and not “Hello world”, one of the correct solutions to this problem would be:

    if (print_r(“Hello World”) && die()) {
    echo ‘Hello’;
    } else {
    echo ‘ world’;
    }

    Cheers! 😉

  2. if(1){
    echo ‘Hello’;

    } else {

    echo ‘ world’;

    }
    or
    if(true){
    echo ‘Hello’;

    } else {

    echo ‘ world’;

    }

  3. andreea.paraschiv

    Well done, guys! I am much impressed with your solutions!

    Good job, Rob, Unu, Srikanth, Prasad and Alex! We’ll publish another challenge for you in justa few days. Stay close!

  4. if(array_map(function($sValue){echo $sValue; }, array(‘Hello’, ‘World’)) && die(exit()))
    {
    echo ‘Hello’;
    }
    else
    {
    echo ‘World’;
    }

  5. None of the solutions you cite (not including replies by others) would work as you are asking for the result of “Hello World” and not “Hello world”. Case matters when in quotes.

    1. andreea.paraschiv

      Ray, be a team players! There are small changes here and there, indeed, to be made. So what’s your solution?

Leave a Comment

Your email address will not be published.

Scroll to Top