PHP的生成器快速实现斐波那契数列 1. [代码] 斐波那契数列 ?php// fib.php function fib($n){ $cur = 1; $prev = 0; for ($i = 0; $i $n; $i++) { yield $cur; $temp = $cur; $cur = $prev + $cur; $prev = $temp; }}$fibs = fib(19);f
1. [代码]斐波那契数列
<?php // fib.php function fib($n) { $cur = 1; $prev = 0; for ($i = 0; $i < $n; $i++) { yield $cur; $temp = $cur; $cur = $prev + $cur; $prev = $temp; } } $fibs = fib(19); foreach ($fibs as $fib) { echo " " . $fib; } # php fib.php