Last active
September 15, 2020 01:02
-
-
Save design-innovations/6ea2e51c0542c3fc43fee33d30dc882e to your computer and use it in GitHub Desktop.
PHP - Generator function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function xrange($start, $limit, $step = 1) { | |
for ($i = $start; $i <= $limit; $i += $step) { | |
yield $i; | |
} | |
} | |
foreach (xrange(1, 9, 2) as $number) { | |
echo "$number "; | |
} | |
// This prints: 1 3 5 7 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment