Click to See Complete Forum and Search --> : Swap the contents of two variables.


AJAX
04-08-2006, 05:30 PM
I've got two variables $foo and $bar and I want to put the contents of $foo into $bar and the contents of $bar into $foo without using any temporary variables.

novaphoenix
04-08-2006, 05:35 PM
list($bar, $foo) = array($foo, $bar);
hope that helps :)

sitehatchery
04-08-2006, 10:11 PM
http://us3.php.net/manual/ar/function.list.php

But, I don't see how this is unlike storing it in a temporary variable. Instead, you are storing it in a variable, then an array, then adding another point in memory with the list construct.

For instance,


$foo = "foo";
$bar = "bar";
$foobar=array($foo, $bar);
$foo="";
$bar="";
list($bar, $foo) = $foobar;
$foobar="";
echo $bar."<br>";
echo $foo;

We started with two variables and added them to an array. We then destroyed the original variables, but a new set of variables exists in memory with the array. Then, we sent the array values to the list construct and destroyed the array. But, you will see that a third set of variables now exist when you print the variables. The $foo and $bar values were reversed, but as I see it, it is not much different than temporary variables. However, it is a nice shortcut. So, thanks for the tip.

felgall
04-08-2006, 10:42 PM
Whatever way you do it needs a temporary variable to hold one while you move the other. Any fancy approaches probably end up using more than the one temporary variable. The simplest solution that uses the least code and the least number of temporary variables is:

$t = $foo; $foo = $bar; $bar = $t;

balloonbuffoon
04-08-2006, 11:10 PM
Whatever way you do it needs a temporary variable to hold one while you move the other.
But novaphoenix's solution works without any temporary variables:list($bar, $foo) = array($foo, $bar);

--Steve

sitehatchery
04-08-2006, 11:22 PM
But that's just the thing. You first have to store it into a variable, and then into an array and then into the list construct. As I've demonstrated, it's actually refering to multiple points in memory. It's really no different from storing it into multiple variables.

balloonbuffoon
04-08-2006, 11:25 PM
Yes, but you, as the coder, don't have to declare an extra variable. And it looks more logical at first glance-- it clearly looks like the values are being swapped.

--Steve

sitehatchery
04-08-2006, 11:36 PM
What's the advantage? It just makes it less logical because you convert a standard variable into an array so that you can convert it into a standard variable again.

check it out:


$foo = "foo";
$bar = "bar";
$yoda="yoda";

list($foo, $yoda, $bar) =array($foo, $bar, $yoda);

echo $foo.",".$yoda.",".$bar;

All it does is take the first value of the array and assign it to the first list variable, and the second and so forth. I can change the variable names to anything I want within the list construct and it yields the same result.

So all you are doing is storing the array variable into a variable with a new name, which can be anything you want, including one that you called before.

balloonbuffoon
04-08-2006, 11:47 PM
I don't know, it just looks more logical to me. But I don't see the advantage of swapping the variable's values in the first place.

--Steve

sitehatchery
04-09-2006, 12:06 AM
I totally agree.

bokeh
04-09-2006, 03:43 AM
What's the advantage? The advantage is there are not a load of temporary variables hanging around.

SpectreReturns
04-09-2006, 04:31 AM
There is a way, but it's faster just to use a temporary variable.

$foo = "hello";
$bar = 1337;

$bar = $bar ^ $foo;
$foo = $bar ^ $foo;
$bar = $bar ^ $foo;

$foo == 1337;
$bar == "hello";

insane
04-09-2006, 07:36 AM
is there anything like the QBasic XOR in php?

SET a = "One"
SET b = "Two"
a XOR b
b XOR a
a XOR b

SpectreReturns
04-09-2006, 04:52 PM
The ^ operator. See my post above yours.