22 September 2013

关键是使用异或操作,将两个变量的值都看成二进制就一目了然了.

1
2
3
4
5
6
7
8
9
10
11
12
13
<meta charset="utf-8">
<?php 
$a = '123abc你好!';
$b = '好啊!456xyz';
echo '$a='.$a.'<br />';
echo '$b='.$b.'<br /><br />';
$a = $a ^ $b;
$b = $b ^ $a;
$a = $a ^ $b;
echo 'swap $a $b<br /><br />';
echo '$a='.$a.'<br />';
echo '$b='.$b.'<br />';
?>