3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gen1() { $x = yield 1; echo "Sup!\n"; $y = yield 2; var_dump($x + $y); } /** function yieldFrom() { $iter = gen1(); foreach($iter as $val) { yield $val; } } /**/ /**/ function yftest() { yield from gen1(); echo "Dawg\n"; yield from gen1(); yield from [1, 2, 3]; } /**/ $g = yftest(); //$g = gen1(); // TEST - Iteration /**/ foreach($g as $val) { var_dump($val); } /**/ // TEST - Sending /** $g->next(); $g->send(10); $g->send(20); /**/
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
int(1) Sup! int(2) int(0) Dawg int(1) Sup! int(2) int(0) int(1) int(2) int(3)
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.21
Parse error: syntax error, unexpected '1' (T_LNUMBER) in /in/T2a7F on line 4
Process exited with code 255.

preferences:
190.4 ms | 402 KiB | 260 Q