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
Release: 2016-07-13 10:01:11
Original
1237 people have browsed it

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;...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template