Use yield from
to create a generator function:
<?php
function countTo4() {
yield from [1, 2, 3];
yield 4;
}
foreach(countTo4() as $number) {
echo $number;
echo "<br>";
}
?>
Try it Yourself »
The yield from
keyword is used to create a generator function. Generator functions act as iterators which can be looped through with a foreach
loop.
The yield from
keyword provides the values from an iterator one by one each time the generator function is called until there are no items left in the iterator, then the generator will move on to the next yield
keyword.
The yield
keyword.
The foreach
keyword.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!