tags:

views:

58

answers:

3

Say for instance I have ...

$var1 = ABC
$var2 = 123

and under certain conditions I want to swap the two around like so...

$var1 = 123
$var2 = ABC

Is there a PHP function for doing this rather than having to create a 3rd variable to hold one of the values then redefining each, like so...

$var3 = $var1
$var1 = $var2
$var2 = $var3

For such a simple task its probably quicker using a 3rd variable anyway and I could always create my own function if I really really wanted to. Just wondered if something like that existed?

+9  A: 

There's no function I know of, but there is a one-liner courtesy of Pete Graham:

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

not sure whether I like this from a maintenance perspective, though, as it's not really intuitive to understand.

Also, as @Paul Dixon points out, it is not very efficient, and is costlier than using a temporary variable. Possibly of note in a very big loop.

However, a situation where this is necessary smells a bit wrong to me, anyway. If you want to discuss it: What do you need this for?

Pekka
+1 that's a nice trick.
Mark E
lol, beat me to it! :p
Thomas Clayson
+1 - found the same, and agree, not sure I like it either - using a temp variable is considerably better - as is writing your own swap method if you find yourself doing this often.
Will A
+1, Beat me to it as well :) I like the idea in principle, but unfortunately the PHP syntax just leaves a bad taste...
Justin Ethier
wow, super quick response. I take my hat off to all of you!is a nice little trick that.
Taylor
@Taylor there is also a question to you in this quick response.
Col. Shrapnel
using it to switch 2 values in a field if some plonker puts values in the form around the wrong way. The values are fairly restricted to what they can be so its easy enough to figure out whether they should have been around the other way. More of a usability thing so it helps out those that aren't great on a computer... or just having a bad moment. :-)
Taylor
@Taylor ah, makes sense.
Pekka
as this is the accepted answer for a question that might educate others, it's worth pointing out just how inefficient this is. You're asking PHP to create an array, only to immediately discard it. I benchmarked this approach against using a temporary variable, and found that using a temp variable is over 7 times faster. I would also argue it makes the intent clearer too, as it's a common idiom!
Paul Dixon
@Paul great information about the benchmark - it's meaningless for one or two operations, but will make a difference in loops. Editing the answer to reflect that.
Pekka
@paul figured as much, simplicity usually is. Good to see an actual benchmark.
Taylor
+1  A: 
list($var1,$var2) = array($var2,$var1);
Thomas Clayson
+1  A: 

Yes, try this:

// Test variables
$a = "content a";
$b = "content b";

// Swap $a and $b
list($a, $b) = array($b, $a);

This reminds me of python, where syntax like this is perfectly valid:

a, b = b, a

It's a shame you can't just do the above in PHP...

Justin Ethier