function Fib($n) { if ($n == 0 || $n == 1) { return 1; } else { return Fib($n - 2) + Fib($n - 1); } }
function Fib($n) { global $FV; if (!is_int($n) || $n < 0) { return 0; } if (!isset($FV[$n])) { $FV[$n] = Fib($n - 2) + Fib($n - 1); } return $FV[$n]; }
<?php /** * Write an integer function Fib(N) that returns the value of N-th term of the sequence of the * Fibonacci numbers. The Fibonacci numbers FK are defined as follows: * F1 = 1, F2 = 1, FK = FK−2 + FK−1, K = 3, 4, … . * sing this function, find five Fibonacci numbers with given order numbers N1, N2, …, N5. */ function Fib($N) { $x = 0; $y = 1; $z = 0; while ($z < $N) { $z = $x + $y; echo $z . "<br />"; $x = $y; $y = $z; } } Fib(50);