An endless machine
A generator that counts forever, and a loop that stops after three.
<?phpfunction naturals() { $n = 1; while (true) { yield $n++; }}foreach (naturals() as $n) { if ($n > 3) { break; } echo $n . PHP_EOL;}echo 'stopped' . PHP_EOL;1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…
- 4…