这个 php 方法假设使用 for 循环将斐波那契数列打印到指定值。我不确定为什么它不起作用?
<?php
function fib ($n) { // a function called fib, declaire variable n (the sequence number)
for ($n=0;$n<30;$n++) {
if ($n < 3) { return $n; } // if n is smaller than 3 return n (1 or 2)
else { return fib ($n - 1) + fib ($n - 2); }
/* if the number is 3 or above do 2 sums (n-1) and (n-2)
and then add the 2 sums together (n-1)+(n-2)
Example Fibonacci number 4
(4-1)+(4-2) = 5
3 + 2 = 5
*/
}
print $n;
?>