Overview - Generator return values
What is it?
In PHP, a generator is a special function that can pause and resume its execution, producing values one at a time. When a generator finishes, it can return a final value using the return statement. This return value is not yielded like other values but can be accessed after the generator completes. It allows the generator to provide a summary or final result beyond the sequence of yielded values.
Why it matters
Without generator return values, you could only get the sequence of yielded items but not any final result or status from the generator. This limits how much information a generator can communicate. Return values let you combine efficient iteration with a meaningful final output, making generators more powerful and flexible in real applications like data processing or pipelines.
Where it fits
Before learning generator return values, you should understand basic PHP functions and how generators yield values. After this, you can explore advanced generator features like delegation with yield from and exception handling inside generators.