Home > Backend Development > PHP Tutorial > PHP does not use a third variable to exchange the values ​​​​of two variables_PHP tutorial

PHP does not use a third variable to exchange the values ​​​​of two variables_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-20 11:13:52
Original
1326 people have browsed it

I once did a php interview question like this: Exchange the values ​​of two variables without using a third variable. Generally, a third intermediate variable is used to exchange the values ​​of the original two variables, but this question requires that the intermediate variable cannot be used, which is also a difficult problem for beginners. Several methods found on the Internet are summarized as follows:

<span //</span><span 字符串版本 结合使用substr,strlen两个方法实现</span>
<span $a</span>="a"<span ;
</span><span $b</span>="b"<span ;
</span><span echo</span> '交换前 $a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
</span><span $a</span>.=<span $b</span><span ;
</span><span $b</span>=<span substr</span>(<span $a</span>,0,(<span strlen</span>(<span $a</span>)-<span strlen</span>(<span $b</span><span )));
</span><span $a</span>=<span substr</span>(<span $a</span>, <span strlen</span>(<span $b</span><span ));
</span><span echo</span> '交换后$a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
 
</span><span echo</span> '-----------------------<br/>'<span ;
 
</span><span //</span><span 字符串版本 使用str_replace方法实现</span>
<span $a</span>="a"<span ;
</span><span $b</span>="b"<span ;
</span><span echo</span> '交换前 $a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
</span><span $a</span>.=<span $b</span><span ;
</span><span $b</span>=<span str_replace</span>(<span $b</span>, "", <span $a</span><span );
</span><span $a</span>=<span str_replace</span>(<span $b</span>, "", <span $a</span><span );
</span><span echo</span> '交换后$a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
 
</span><span echo</span> '-----------------------<br/>'<span ;
 
</span><span //</span><span 字符串版本 结合使用list方法和array实现</span>
<span $a</span>="a"<span ;
</span><span $b</span>="b"<span ;
</span><span echo</span> '交换前 $a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
</span><span list</span>(<span $b</span>,<span $a</span>)=<span array</span>(<span $a</span>,<span $b</span><span );
</span><span echo</span> '交换后$a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
 
</span><span echo</span> '-----------------------<br/>'<span ;
 
</span><span //</span><span 字符串和数字都适用 使用异或运算</span>
<span $a</span>='a'<span ;
</span><span $b</span>='b'<span ;
</span><span echo</span> '交换前 $a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
</span><span $a</span>=<span $a</span>^<span $b</span><span ;
</span><span $b</span>=<span $b</span>^<span $a</span><span ;
</span><span $a</span>=<span $a</span>^<span $b</span><span ;
</span><span echo</span> '交换后$a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
 
</span><span echo</span> '-----------------------<br/>'<span ;
 
</span><span //</span><span 只适用于数字</span>
<span $a</span>=3<span ;
</span><span $b</span>=5<span ;
</span><span echo</span> '交换前 $a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />'<span ;
</span><span $a</span>=<span $a</span>+<span $b</span><span ;
</span><span $b</span>=<span $a</span>-<span $b</span><span ;
</span><span $a</span>=<span $a</span>-<span $b</span><span ;
</span><span echo</span> '交换后$a:'.<span $a</span>.',$b:'.<span $b</span>.'<br />';
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440328.htmlTechArticleI once did a PHP interview question like this: Exchange the values ​​​​of two variables without using a third variable . Generally, a third intermediate variable is used to realize the value exchange of the original two variables...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template