1 <?php 2 $str = '1234'; 3 $ar =str_split($str); 4 perm($ar, 0, count($ar)-1); 5 6 function perm(&$ar, $now, $length) { 7 if($now == $length){ 8 $str=substr(join("",$ar),0,3); 9 echo $str, PHP_EOL; 10 }else { 11 for($i=$now; $i<=$length; $i++) { 12 swap($ar[$now], $ar[$i]); 13 perm($ar, $now+1, $length); 14 swap($ar[$now], $ar[$i]); 15 } 16 } 17 } 18 function swap(&$a, &$b) { 19 $c = $a; 20 $a = $b; 21 $b = $c; 22 }