PHP: Swap Variables One Liner
Pete
Here's a nifty one liner in PHP which let's you swap 2 variables values.
Much like the PHP XOR swap there's no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something.
/* Pete Graham */
Posted in Tech, php, programming |
August 22nd, 2007 at 12:37 pm
… except that it makes it easier to write another one-liner like this:
if(whatever) list($a,$b)=array($b,a);
December 27th, 2007 at 5:09 pm
[…] And then one day while perusing the internet I found an even easier solution on Optimus Pete - PHP Swap Variables One Liner. […]
July 28th, 2008 at 8:10 pm
[…] It’s always a good thing to search for something cool that you’ve found before blogging it, to make sure that others have not already done the same! It even helped me see a PHP trick that should be familiar to seasoned C/C++ programmers… […]
October 24th, 2008 at 5:06 pm
Doesn’t this just introduce the overhead of creating a temporary array? If the value of XOR swapping is that it saves memory, wouldn’t doing this just waste double the memory of making a single temporary variable?