两数交换
<?php $a='hello'; $b='world'; /* * list 把数组中的值赋给一些变量 */ function swap1(&$a,&$b){ list($b,$a)=array($a,$b); } /* * 交换两个变量的值应该使用中间变量 */ function swap2(&$a, &$b){ $temp=$a; $a=$b; $b=$temp; } //array_reverse 返回一个单元顺序相反的数组 function swap3(&$a, &$b){ $temp=array_reverse([$a,$b]); $a=$temp[0]; $b=$temp[1]; } //利用数组处理 function swap4(&$a, &$b){ $b=[$a,$b]; $a=$b[1]; $b=$b[0]; } //如果是数值 function swapInt(&$a, &$b){ $b=$a+$b; $a=$b-$a; $b=$b-$a; }