A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate.
<?php
/**
* Helper function to get $count of even numbers.
*/
function func($count) {
for ($i = 0; $i < $count; $i++) {
yield $i * 2;
}
}
foreach (func(100) as $value) {
echo $value . "\n";
}