When writing PHP applications, you may need to understand how yield streams values instead of building all results in memory. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The Generators vs Arrays: Memory Usage operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is Generators vs Arrays: Memory Usage in PHP?
Understand how yield streams values instead of building all results in memory.
Its syntax is:
function numbers(): Generator { yield 1; }
A generator can reduce peak memory use when values are produced lazily, but building a large array inside the generator defeats that benefit. Generators cannot be counted directly with count().
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
function numberStream(): Generator { for ($number = 1; $number <= 3; $number++) { yield $number; } }
foreach (numberStream() as $number) { echo $number . PHP_EOL; }
Output:
1
2
3
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$arrayValues = range(1, 5);
$generatorValues = (function () { foreach (range(1, 5) as $value) { yield $value; } })();
echo count($arrayValues) . PHP_EOL;
foreach ($generatorValues as $value) { echo $value . " "; }
Output:
5
1 2 3 4 5
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Stream large CSV rows one at a time rather than loading the entire file
Stream large CSV rows one at a time rather than loading the entire file. Save the code in a PHP file and adapt the sample data to your project.
<?php
function streamSequence(int $maximum): Generator { for ($current = 1; $current <= $maximum; $current++) { yield $current; } }
$total = 0;
foreach (streamSequence(1000) as $number) { $total += $number; }
echo "Sum: " . $total;
Output:
Sum: 500500
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
A generator can reduce peak memory use when values are produced lazily, but building a large array inside the generator defeats that benefit. Generators cannot be counted directly with count().
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
Generators vs Arrays: Memory Usage | Understand how yield streams values instead of building all results in memory |
| Related functions | Arrays support fast repeated indexing; generators support sequential lazy iteration. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming the example covers every input type | A generator can reduce peak memory use when values are produced lazily, but building a large array inside the generator defeats that benefit. Generators cannot be counted directly with count(). |
| Confusing the method with a similar operation | Arrays support fast repeated indexing; generators support sequential lazy iteration. |
| Ignoring a failed or absent result | Check return values before using them in subsequent operations. |
Key Takeaways
Generators vs Arrays: Memory Usagehelps you understand how yield streams values instead of building all results in memory.- A generator can reduce peak memory use when values are produced lazily, but building a large array inside the generator defeats that benefit. Generators cannot be counted directly with count().
- Arrays support fast repeated indexing; generators support sequential lazy iteration.
- Use the practical example as a starting point, but validate real application inputs.
Generators vs Arrays: Memory Usage is useful when you need to understand how yield streams values instead of building all results in memory. Use its documented rules and the examples above to choose the right behavior for your data.