Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 09:10:16
Original
992 people have browsed it

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

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

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

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!