PHP Generators
Also Known As
PHP yield
generator function PHP
lazy evaluation PHP
PHP iterator
TL;DR
Functions using yield that produce values lazily — one at a time — instead of building a complete array in memory.
Explanation
A generator function uses yield to return values one at a time, pausing execution between yields. The caller receives a Generator object implementing Iterator. Generators consume O(1) memory regardless of the sequence length — ideal for large datasets, file streaming, and infinite sequences. yield from delegates to another generator or iterable. send() passes values back into a paused generator. PHP 5.5+ supports generators; PHP 7.0 added return values from generators.
Common Misconception
✗ Generators are only useful for infinite sequences. Generators benefit any situation where you need to process a large dataset without loading it all into memory at once.
Why It Matters
Loading a 1M-row CSV into an array requires hundreds of MB of RAM. A generator processes it line by line in constant memory — the difference between a working script and an out-of-memory crash.
Common Mistakes
- Calling a generator function and expecting an array — it returns a Generator object; use foreach or iterator_to_array().
- Using iterator_to_array() on a large generator — defeats the memory benefit by materialising the full sequence.
- Not using yield from when composing generators — manually looping and yielding is verbose and slower.
Avoid When
- Do not use generators when you need random access to elements — generators are forward-only.
- Avoid calling iterator_to_array() on a generator for large datasets — it loads the full sequence into memory.
When To Use
- Use generators for processing large files, database result sets, or API pagination where loading everything at once would exhaust memory.
- Use generators when you need a lazy sequence — producing values only when requested.
Code Examples
✗ Vulnerable
// Loads entire CSV into memory — crashes on large files
function readCsvRows(string $file): array {
$rows = [];
$handle = fopen($file, 'r');
while (($row = fgetcsv($handle)) !== false) {
$rows[] = $row; // 1M rows = hundreds of MB RAM
}
return $rows;
}
✓ Fixed
// Generator: reads CSV line by line — O(1) memory
function readCsvRows(string $file): \Generator {
$handle = fopen($file, 'r');
while (($row = fgetcsv($handle)) !== false) {
yield $row;
}
fclose($handle);
}
foreach (readCsvRows('million_rows.csv') as $row) {
processRow($row); // only one row in memory at a time
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
31 Mar 2026
Views
17
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 6
Google 2
Unknown AI 2
ChatGPT 1
Ahrefs 1
Meta AI 1
Also referenced
How they use it
crawler 12
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Replace array-returning functions that process large datasets with generators using yield
📦 Applies To
PHP 5.5+
web
cli
queue-worker
🔍 Detection Hints
Function returning large array from file/DB that could be a generator
Auto-detectable:
✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Function
Tests: Update