PHP theorytheory 0/50 · 0%
Fundamentals · medium

25. Generators & yield

Lazy iteration.

Generators let a function produce a sequence of values lazily using the yield keyword, without building the entire result set in memory. Calling a generator function returns a Generator object implementing Iterator.

Generators are ideal for processing large datasets, reading big files line by line, or building infinite sequences, since each value is computed only when requested.

Check your understanding

  1. 1. Which keyword produces a lazy value in a generator?

  2. 2. What interface does a Generator implement?