• We are trusted partners of:

PHP Challenge - INNOBYTE - Serious Web Development

Posted by | July 08, 2013 | Development, Team | 10 Comments

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?

«Go back

10 Comments

  • Alex Rusu says:

    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! 😉

  • unu says:

    if(printf(“Hello “)) {
    }
    else{
    echo “World”;}

  • Prasad Bodas says:

    if (printf(‘Hello’) < 0) {

    echo 'Hello';

    } else {

    echo 'world';

    }

  • Srikanth says:

    if(1){
    echo ‘Hello’;

    } else {

    echo ‘ world’;

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

    } else {

    echo ‘ world’;

    }

  • Rob V says:

    if (exit(“Hello World”)) {
    echo “Hello “;
    }else{
    echo ” world”;
    }

  • andreea.paraschiv says:

    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!

  • Jörn B. says:

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

  • Claudiu says:

    if(eval(“echo ‘Hello';”) && ‘a’==’b’) {

    echo ‘Hello';

    } else {

    echo ‘ world';

    }

  • Ray Andrews says:

    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.

    • andreea.paraschiv says:

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

Leave a Reply

Your email address will not be published.