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

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

WBOY
WBOYOriginal
2016-07-13 10:01:111192browse

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

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

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/972511.htmlTechArticle不用新变量交换两个变量的值,交换变量 面试的时候经常碰到这个问题,特地研究了下,如题 $a = 1; $b = 2; 方法1: $a ^= $b;$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