3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gen($a = 0) { yield 1; yield 2; yield 3; yield 4; yield 5; } function bar($gen) { yield from $gen; } $gen = gen(); $gens[] = bar($gen); $gens[] = bar($gen); do { foreach ($gens as $g) { echo "Current: "; var_dump($g->current()); // Notice - We are advancing the underlying gen, not either of the things // gens in the array. If we delegate properly, then our other generators // should be implicitly advancing as well. $gen->next(); } } while($gen->valid()); var_dump($gens[1]->valid());
Output for 7.0.1 - 7.0.20, 7.1.0 - 7.1.25, 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.18, 8.3.0 - 8.3.6
Current: int(1) Current: int(2) Current: int(3) Current: int(4) Current: int(5) Current: int(5) bool(true)
Output for 7.0.0
Current: int(1) Current: int(2) Current: int(3) Current: int(4) Current: int(5) Current: NULL bool(false)
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Parse error: syntax error, unexpected '$gen' (T_VARIABLE) in /in/JFrIb on line 10
Process exited with code 255.

preferences:
183.23 ms | 402 KiB | 198 Q