Skip to content

Instantly share code, notes, and snippets.

@lorenzo
Created January 10, 2014 08:35
Show Gist options
  • Save lorenzo/8348639 to your computer and use it in GitHub Desktop.
Save lorenzo/8348639 to your computer and use it in GitHub Desktop.
A non-recursive and stack overflow safe fibonacci function in PHP
<?php
function fib() {
list($x, $y) = [0, 1];
while(1) {
yield $y;
list($x, $y) = [$y, $x + $y];
}
}
$generator = fib();
$i = 0;
while($i++ < 10) {
echo $generator->send(null) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment