I am a software engineer.
I live in the UK and currently work for DeviantArt.
This blog lets me vent some of the tech junk that is always on my mind. If it does nothing else
I consider it a courtesey to my wife.
You should note the date on anything you read here. There are things written here in the past that I leave for historical purposes rather than because they remain accurate or even consistent with my current opinions.
In case it's not obvious, this is my personal blog. The views expressed on these pages are mine alone and not those of my employer.
An exercise most programmers are shown when first being introduced to bit arithmetic is how to swap the values of variables without using a third. The answer is the XOR (eXclusive OR) trick.
For some reason this came to mind this morning and I wondered - what happens in PHP if you try to XOR non integer data types? I could probably have looked it up but a 2 minute script showed me the answer: PHP casts whatever value to an int before performing the operation. So no, you can't neatly swap two arrays or objects or even strings without a temporary variable. Oh well.
For those who've not come across it before. Here is the magic I'm talking about. If you don't believe it works at first, grab a pencil and paper and work through the binary maths for yourself...
::php::
$a = 1;
$b = 2;
echo"a: ".$a.", b: ".$b."\n";
// Do the swap$a ^= $b;
$b ^= $a;
$a ^= $b;
// All doneecho"a: ".$a.", b: ".$b."\n";