PHP yield from Keyword

❮ PHP Keywords

Example

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 »

Definition and Usage

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.


Related Pages

The yield keyword.

The foreach keyword.


❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.