Content · Programming · PHP

PHP Generators vs Arrays: Memory Usage

Understand Generators vs Arrays: Memory Usage in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

2 views Published Oct 16, 2026 Updated Oct 16, 2026

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 ApproachWhen to Use It
Generators vs Arrays: Memory UsageUnderstand how yield streams values instead of building all results in memory
Related functionsArrays support fast repeated indexing; generators support sequential lazy iteration.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming the example covers every input typeA 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 operationArrays support fast repeated indexing; generators support sequential lazy iteration.
Ignoring a failed or absent resultCheck return values before using them in subsequent operations.

Key Takeaways

  • Generators vs Arrays: Memory Usage helps 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.

Share this resourceSend this page to someone who may find it useful.