Skip to content

Instantly share code, notes, and snippets.

@ykocaman
Last active April 3, 2019 13:35
Show Gist options
  • Save ykocaman/4c4d2cded40a81bf1fbc630d75cf2be0 to your computer and use it in GitHub Desktop.
Save ykocaman/4c4d2cded40a81bf1fbc630d75cf2be0 to your computer and use it in GitHub Desktop.
<?php
function fibonacci($limit){
$first = 0;
$second = 1;
for($i = 0; $i < $limit; $i++){
yield $first;
list($first, $second) = [$second, $first + $second];
}
}
// var_dump(iterator_to_array(fibonacci(10)));
foreach (fibonacci(10) as $key => $result){
echo sprintf('%s. number: %s%s', ++$key, $result, PHP_EOL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment