tags:

views:

74

answers:

2

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

$a^=$b^=$a^=$b;
+6  A: 

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);
gregjor
+1 But that should work for any types, not only scalars.
nikic
Yes it does... I wasn't sure list() worked with arrays but I tried it and it does work.
gregjor
A: 

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

matthewyang