Method: 1. Use the statement "$a=$a^$b;$b=$b^$a;$a=$a^$b;"; 2. Use $b=explode( "|",$a."|".$b);$a=$b[1];$b=$b[0];" statement; 3. Use "list($b,$a)=array ($a,$b);".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php does not use a third variable to exchange the values of two variables
Method 1: Use XOR operation
<?php header("Content-type:text/html;charset=utf-8"); $a=125; $b=854; echo '交换前 :<br />'; echo $a."<br>"; echo $b."<br>"; $a=$a^$b; $b=$b^$a; $a=$a^$b; echo '<br>交换后 :<br />'; echo $a."<br>"; echo $b; ?>
Output result:
##Method 2:
<?php header("Content-type:text/html;charset=utf-8"); $a=12; $b=85; echo '交换前 :<br />'; echo $a."<br>"; echo $b."<br>"; $b=explode("|", $a."|".$b); $a=$b[1]; $b=$b[0]; echo '<br>交换后 :<br />'; echo $a."<br>"; echo $b; ?>
Method 3:
<?php header("Content-type:text/html;charset=utf-8"); $a=126; $b=85; echo '交换前 :<br />'; echo $a."<br>"; echo $b."<br>"; list($b,$a)=array($a,$b); echo '<br>交换后 :<br />'; echo $a."<br>"; echo $b; ?>
PHP Video tutorial》
The above is the detailed content of How to exchange the values of two variables in php without using a third variable. For more information, please follow other related articles on the PHP Chinese website!