Home  >  Article  >  Backend Development  >  php实现变量换值,该如何处理

php实现变量换值,该如何处理

WBOY
WBOYOriginal
2016-06-13 13:29:18742browse

php实现变量换值
用PHP写一段代码,实现不使用第3个变量,交换$a、$b的值,$a、$b的初始值自己定。

------解决方案--------------------
这个百度多的是啊。。。。

这两个数必定是 整型数啊。。。
------解决方案--------------------
a = a+b
b = a-b
a = a-b
------解决方案--------------------

PHP code

$a = "abcd";
$b = "1234";
echo "初始化时 a=$a,b=$b<br>";
$a = $a . $b;
$b = strlen( $b );
$b = substr( $a, 0, (strlen($a) - $b ) );
$a = substr( $a, strlen($b) );
echo "交换后 a=$a,b=$b<br>";
<br><font color="#e78608">------解决方案--------------------</font><br>php已经提供了绝好的途径<br><br>$a = "abcd";<br>$b = "1234";<br><br>list($b, $a) = array($a, $b);<br><br>echo "$a $b"; //1234 abcd <div class="clear">
                 
              
              
        
            </div>
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