Home >Backend Development >PHP Tutorial >Swap the values ​​of two variables without using new variables, Swap Variables_PHP Tutorial

Swap the values ​​of two variables without using new variables, Swap Variables_PHP Tutorial

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

You don’t need to exchange the values ​​of two variables with new variables. Exchange variables

I often encounter this problem during interviews, so I studied it specifically, as in the question

$a = 1;

$b = 2;

Method 1:

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

Method 2:

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

Method 3: (If the variable is an integer)

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/972511.htmlTechArticleYou don’t need to exchange the values ​​​​of two variables with new variables. This problem is often encountered when exchanging variables during interviews, so I studied it specially. Below, as in the question $a = 1; $b = 2; Method 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