2

Is it safe to use this kind of variable swapping in php ?

$a^=$b^=$a^=$b;
johnlemon
  • 20,761
  • 42
  • 119
  • 178

2 Answers2

6

No, because the variables may not be types that can be XORd the way you expect. The PHP idiom for swapping two variables (of any scalar type) in one line is:

list($a, $b) = array($b, $a);

Or simply:

[$a, $b] = [$b, $a];
gregjor
  • 21,802
  • 1
  • 23
  • 16
-2

Only correct when both are integer. It's readability is poor,and efficiency is not good too,why use it?