• We are trusted partners of:

Second PHP Challenge - INNOBYTE - Serious Web Development

Posted by | July 15, 2013 | Development, Fun | 3 Comments

As the challenge we’ve given you last week was a real hit, we want to provoke you again!

So… you are given 2 integral numbers: a and b.

Request: Write a code sequence that interchanges the values of  these 2, without using additional variables.

«Go back

3 Comments

  • Liviu says:

    $a= $b-$a;
    $b=$a-$b;
    $a=$a+$b;

  • unu says:

    $a ^= $b ^= $a ^= $b;

  • andreea.paraschiv says:

    Well done, guys!

    Our solutions were:
    1.
    $a = $a + $b
    $b = $a – $b
    $a = $a – $b

    2.
    $a = $a * $b;
    $b = $a / $b;
    $a = $a / $b;

    3.
    $a = $a ^ $b;
    $b = $a ^ $b;
    $a = $a ^ $b;

    4.
    list($a, $b) = array($b, $a);
    OR
    list($a,$b) = array_reverse(array($a,$b));

    5.
    extract(array(‘a’=>$b, ‘b’=>$a));

    6.
    file_put_contents(‘a.txt’,$a);
    file_put_contents(‘b.txt’,$b);

    $a = file_get_contents(‘b.txt’);
    $b = file_get_contents(‘a.txt’);

    7.
    $a = $a <> strlen(decbin($b));
    $a = $b << (strlen(decbin($a)) – strlen(decbin($b))) ^ $a;

    So.. when it comes to web development, we do get creative!

Leave a Reply

Your email address will not be published.