PHP VERSION: 8.2.18

generator_delegation_array.php

<?php
// generator calls "sub" generators using "yield from"

function foo() {
    yield 
1;
    yield from [
2,3,4];
    yield 
5;
}
function 
bar() {
    yield 
'A';
    yield from [
'B','C','D'];
    yield 
'E';
    
// never gets returned in this example
    
return 42;        
}
function 
both() {
    yield from 
foo();
    yield from 
bar();
    yield 
'DONE';
}

$a both();

foreach (
$a as $value) {
    echo 
"$value\n";
}

Output


1
2
3
4
5
A
B
C
D
E
DONE
SOURCE CODE