3v4l.org

run code in 500+ PHP versions simultaneously
<?php function arrayProvider() { yield [ 'one', 'two', 'three', ]; yield [ 'four', 'five', 'six', ]; yield [ 'seven', 'eight', 'nine', ]; } function iterateValues(array $array) { foreach ($array as $value) { yield $value; } } function g1() { foreach (arrayProvider() as $array) { $iterator = iterateValues($array); if ($iterator?->valid()) { yield from $iterator; } } } function g2() { yield from g1(); } foreach (g2() as $s) { echo $s . PHP_EOL; }
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.32, 8.3.0 - 8.3.32, 8.4.1 - 8.4.24, 8.5.0 - 8.5.9
one two three five six eight nine

preferences:
77.82 ms | 1025 KiB | 4 Q