Generator (computer programming): Difference between revisions

Content deleted Content added
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 7 templates: hyphenate params (4×);
Line 275:
The community of PHP implemented generators in PHP 5.5. Details can be found in the original [https://wiki.php.net/rfc/generators Request for Comments: Generators].
 
Infinite Fibonacci sequence:
<syntaxhighlight lang="php">
function fibonacci()
Line 290 ⟶ 291:
foreach (fibonacci() as $number) {
echo $number, "\n";
}
</syntaxhighlight>
 
Fibonacci sequence with limit:
<syntaxhighlight lang="php">
function fibonacci($limit)
{
yield $a = $b = $i = 1;
while (++$i < $limit) {
yield $a = ($b = $a + $b) - $a;
}
}
 
foreach (fibonacci(10) as $number) {
echo "$number\n";
}
</syntaxhighlight>