3v4l.org

run code in 300+ PHP versions simultaneously
<?php $generator = (function() { $count = 3; echo "Start\n"; while(true) { yield; // suspend the function (the generator) echo "Are there results?\n"; $count--; if ($count === 0) { return; // We received the results, we stop } } })(); $generator->current(); // Initiates processing do { echo "Do something else\n"; $generator->next(); // Restart the function (resume at "yield") } while ($generator->valid()); // Has the function finished? echo "End\n";
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.23, 8.4.1 - 8.4.10
Start Do something else Are there results? Do something else Are there results? Do something else Are there results? End

preferences:
52.28 ms | 406 KiB | 5 Q