<?php declare(strict_types=1); function generator(): iterable { echo "generate 1\n"; yield 'some text'; echo "generate 2\n"; yield 12; echo "generate 3\n"; $i = 1; yield $i++; echo "generate 4\n"; yield $i; } foreach (generator() as $value) { echo "----------\n"; echo "$value\n"; }