Home  >  Article  >  php教程  >  不用新变量交换两个变量的值,交换变量

不用新变量交换两个变量的值,交换变量

WBOY
WBOYOriginal
2016-06-13 09:10:16987browse

不用新变量交换两个变量的值,交换变量

面试的时候经常碰到这个问题,特地研究了下,如题

$a = 1;

$b = 2;

 

方法1:

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

 

方法2:

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

 

方法3:(如果变量是整型)

$a=$a+$b; $b=$a-$b; $a=$a-$b;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn