Take turns
The generator and the foreach loop both log. Watch them take turns.
<?phpfunction make() { foreach (['tea', 'soup'] as $item) { echo "make $item" . PHP_EOL; yield $item; } echo 'machine empty' . PHP_EOL;}foreach (make() as $item) { echo "use $item" . PHP_EOL;}echo 'done' . PHP_EOL;1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…
- 4…
- 5…
- 6…