Created
August 23, 2019 08:13
-
-
Save iamlucianojr/b167d40b8641af08a4be1716b7ee0839 to your computer and use it in GitHub Desktop.
Generators/yield PHP
This file contains hidden or 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
function gen_one_to_three() | |
{ | |
for ($i = 1; $i <= 3; $i++) { | |
// Note that $i is preserved between yields. | |
yield $i; | |
} | |
} | |
$generator = gen_one_to_three(); | |
foreach ($generator as $value) { | |
echo "$value\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment