3v4l.org

run code in 300+ PHP versions simultaneously
<?php function nums() { $ary = [24,25,26,27,28,29,30,31,32]; $i = 0; foreach ($ary as $x) { $cmd = (yield $x); // yield expr var_dump($cmd); if ($cmd == 'stop') { return; // exit generator } } } $gen = nums(); foreach ($gen as $value) { echo "$value\n"; if ($value == 30) { // don't let it get to 32 $gen->send('stop'); } }

preferences:
34.08 ms | 402 KiB | 5 Q