The php bit operator ^ evaluates the string

$a = "abc";  
$b= "def";  
  
$a = $a^$b;  
$b = $b^$a;  
$a = $a^$b; 


echo $b; //abc
echo $a; //def

explain the calculation process, why variables are exchanged

Php
Mar.10,2021

binary XOR exchange, any language is the same

clipboard.png

refer to https://blog.csdn.net/zxm1306.


1^1=0
0^0=0
1^0=1
0^1=1
$b^($a^$b)=$a^($b^$b)=$a
$a^($a^$b)=$b^($a^$a)=$b

suppose the binary value of $an is 1000 $b is 1010

// $a,$b  
//$a = $a^$b; // 1000  1010
//echo $a;    // 0010
//$b = $a^$b;      //1010 0010 =1000
//echo $b;   //1000
//$a = $a^$b;  //  0010  1000 = 1010
//echo $a; // 1010
Menu