Skip to content

Instantly share code, notes, and snippets.

@kriskornel
Last active May 19, 2018 04:34
Show Gist options
  • Save kriskornel/d978be31f66efdc0683d96f5f0193355 to your computer and use it in GitHub Desktop.
Save kriskornel/d978be31f66efdc0683d96f5f0193355 to your computer and use it in GitHub Desktop.
Fibonnaci in PHP using yield
function getFibonacci()
{
$i = 0;
$k = 1; //first fibonacci value
// yield $k;
while(true)
{
$k = $i + $k;
$i = $k - $i;
yield $k;
}
}
$y = 0;
foreach(getFibonacci() as $k)
{
echo $k . "<br/>";
$y++;
if($y > 30)
{
break; // infinite loop prevent
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment