How to interchange 2 variables? A simple question with a simple answer. Most people will do it like this:

// interchanging using a temporary variable
$a = 2;
$b = 3;

$temp = $a;
$a = $b;
$b = $temp;

echo $a . ' ' . $b . PHP_EOL; // will display 3 2

Quite elementary. But it can be done in a much more elegant manner, using arithmetical (addition & subtraction) or logical (xor) operators.
First of all, let’s interchange the variables using addition and subtraction. The code will look like this:

// arithmentical operations
$a = 2;
$b = 3;

$a = $a + $b; // a = 5, b = 3
$b = $a - $b; // a = 5, b = 2
$a = $a - $b; // a = 3, b = 2

echo $a . ' ' . $b . PHP_EOL;

…or, in a more “hacker” style…

// arithmetical operations - one line
$a = 2;
$b = 3;

$a = $a + $b - ( $b = $a );

echo $a . ' ' . $b . PHP_EOL;

But variable interchange can also done using the XOR operator. This is also supposed to be the fastest way.

// xor

$a = 2; // 10 (in binary)
$b = 3; // 11 (in binary)

$a = $a ^ $b; // a = 01, b = 11
$b = $a ^ $b; // a = 01, b = 10
$a = $a ^ $b; // a = 11, b = 10

echo $a . ' ' . $b . PHP_EOL;

Again, one line variable interchange, this time using xor.

// xor - one line
$a = 10;
$b = 5;

$a = $a ^ $b ^ ( $b = $a );

echo $a . ' ' . $b . PHP_EOL;

What are these good for? Well, let’s see, you can post them on your blog late at night when you’re not sleepy and pretend you know everything. Of course, this isn’t my case, since is not *that* late at night.