Aug 15 2007

PHP Swap (Simple Swapping)

php swapping

PHP Simple Swap >>

Hello programmers,

You know list function in php and array function. Just
type like below for swapping two variables $a and $b.

list($b,$a) = array($a,$b);

The above code is equivalent to:

$temp = $a;

$a = $b ;

$b= $temp;

Here we shorten the three line of code into a single line.

TAGS:

3 Comments on this post

Trackbacks

  1. ruX said:

    Good idea :)
    AFIK, this construction needs more memory.

    August 27th, 2008 at 12:55 am
  2. miah said:

    $a = ‘bar’;
    $b = ‘foo’;

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

    echo $a . $b;

    // its faster and needs less memory ^^

    February 23rd, 2009 at 10:00 pm
  3. Blog SEO said:

    Nice trick, thanks :)

    miah, I like your trcik too ;)

    Thanks guys !

    Tiger

    May 14th, 2009 at 7:54 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Recent Comments

  • Sajith M.R: Press shift and enter key for a new line in chat
  • Borellus: Nice little function there, I think I may try to use it at some point.
  • Abhishek: Can we have newline characters…. i want something like this Line 1 Line 2 Line 3
  • Joanne Cox: Thanks for sharing this; your input is appreciated and has made me change my opinion slightly. About the...
  • Saboor: hi i also want to implement a chatting like Gmail or FaceBook , please, email me the source code on...

Recent Posts