PHP yield from Keyword

PHP function Keyword
PHP yield Keyword

In this article, you will learn how to create a generator function in PHP. 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.

examples of the YIELD FROM function

Example 1. In this example, we 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>";
}
?>
PHP function Keyword
PHP yield Keyword
en English
X
Scroll to Top