I just ran across this snippet of code for swapping the values of two variables in PHP:
<?php
$a = ‘bar’;
$b = ‘foo’;
$a = $a ^ $b;
$b = $a ^ $b;
$a = $a ^ $b;
echo $a . $b;
I understand the concept in binary; does this always work on strings? How?
PHP applies bitwise operators to strings by applying it to each character individually.
PHP: Bitwise Operators:
This will work if both strings have the same number of characters, or more precisely the same number of bytes. If the above quote is really precise, then it may only work for ASCII-only strings.