PHP yield Keyword
Example
Use yield
to create a generator function:
<?php
function countTo3() {
yield "1";
yield "2";
yield "3";
}
foreach(countTo3() as $number) {
echo
$number;
echo "<br>";
}
?>
Try it Yourself »
Definition and Usage
The yield
keyword is used to create a generator function. Generator functions act as
iterators which can be looped through with a foreach
loop.
The value given by the yield
keyword is used as a value in one of the iterations of the
loop.
Related Pages
The foreach
keyword.
The yield from
keyword.
❮ PHP Keywords