3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gen($x) { yield $x; return $x * 5; } function yf() { $x = yield from gen(1); echo "x is $x\n"; $y = yield from gen(2); echo "y is $y\n"; return yield from gen(42); } $g = yf(); foreach($g as $val) { var_dump($val); } echo "====================\n"; var_dump($g->getReturn());
Output for 7.0.0 - 7.0.33, 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) x is 5 int(2) y is 10 int(42) ==================== int(210)
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
Fatal error: Generators cannot return values using "return" in /in/tHA6N on line 5
Process exited with code 255.

preferences:
211.03 ms | 402 KiB | 323 Q