开发者

Is it safe to use xor for variable swapping in PHP

开发者 https://www.devze.com 2023-01-20 09:36 出处:网络
Is it safe to use this kind of variable swapp开发者_开发技巧ing in php ? $a^=$b^=$a^=$b; No, because the variables may not be types that can be XORd the way you expect. The PHP idiom for swapping two

Is it safe to use this kind of variable swapp开发者_开发技巧ing in php ?

$a^=$b^=$a^=$b;


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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消