1 <?php 2 /*=============裴波那契数列=============*/ 3 //使用递归 4 function fibonacci_recursive($n){ 5 if($n==1||$n==2){ 6 return 1; 7 }else{ 8 return fibonacci($n-1)+fibonacci($n-2); 9 } 10 } 11 12 //不使用递归 13 function fibonacci($n){ 14 $base1 = 1; 15 $base2 = 2; 16 if($n==1||$n==2){ 17 return 1; 18 }else{ 19 for($i=3;$i<$n;$i++){ 20 $temp = $base2; 21 $base2 = $temp+$base1; 22 $base1 = $temp; 23 } 24 return $base2; 25 } 26 } 27 echo fibonacci(6); 28 echo '<br/>'; 29 echo fibonacci_recursive(6);