The yield expression turns a function into a generator that produces items lazily when iterated.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Generators and yield Works
The yield expression turns a function into a generator that produces items lazily when iterated.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
function digits(): Generator { yield 1; yield 2; yield 3; }
foreach (digits() as $digit) { echo $digit; }
Output:
123
The yield expression turns a function into a generator that produces items lazily when iterated.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
function stockLevels(): Generator { yield "SKU-1" => 9; yield "SKU-2" => 4; }
foreach (stockLevels() as $sku => $level) { echo "$sku: $level\n"; }
Output:
SKU-1: 9
SKU-2: 4
Generator execution begins only when iteration starts and the generator should not be assumed rewindable.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
function evenNumbers(int $maximum): Generator { for ($number = 2; $number <= $maximum; $number += 2) { yield $number; } }
foreach (evenNumbers(8) as $number) { echo $number . " "; }
Output:
2 4 6 8
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Generator execution begins only when iteration starts and the generator should not be assumed rewindable.
- The yield expression turns a function into a generator that produces items lazily when iterated.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Generators and yield | The yield expression turns a function into a generator that produces items lazily when iterated. |
| Alternative approach | Arrays are simpler for small sequences where repeated traversal is needed. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Generators and yield | Generator execution begins only when iteration starts and the generator should not be assumed rewindable. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Arrays are simpler for small sequences where repeated traversal is needed. |
Key Takeaways
- The yield expression turns a function into a generator that produces items lazily when iterated.
- Generator execution begins only when iteration starts and the generator should not be assumed rewindable.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Generators and yield is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.