Generators help create values one at a time without using a lot of memory. They make it easier to work with big data step by step.
When you want to process large lists without loading everything into memory.
When you need to read big files line by line.
When you want to create a sequence of numbers or data on the fly.
When you want to pause and resume a function to save resources.
When you want to improve performance by not waiting for all data at once.
Syntax
PHP
<?php
function generatorExample() {
yield1;
yield2;
yield3;
}
foreach (generatorExample() as $value) {
echo $value . "\n";
}
yield is used to return values one by one.
Generators pause after each yield and resume when needed.
Examples
This example shows a simple generator that yields numbers 1 to 3.
PHP
<?php
function countToThree() {
yield1;
yield2;
yield3;
}
foreach (countToThree() as $number) {
echo $number . "\n";
}
This generator creates an infinite sequence of numbers but we stop after 5.
PHP
<?php
function infiniteNumbers() {
$i = 0;
while (true) {
yield $i++;
}
}
foreach (infiniteNumbers() as $num) {
if ($num > 5) break;
echo $num . "\n";
}
Sample Program
This program uses a generator to produce numbers from 1 to 1,000,000 but only prints the first 5. It shows how generators save memory by not creating the whole list at once.
PHP
<?php
function bigDataGenerator() {
for ($i = 1; $i <= 1000000; $i++) {
yield $i;
}
}
$count = 0;
foreach (bigDataGenerator() as $value) {
echo $value . "\n";
$count++;
if ($count >= 5) break; // Stop after 5 to avoid too much output
}
OutputSuccess
Important Notes
Generators are memory efficient because they produce values only when needed.
You can use yield inside loops to generate large sequences easily.
Generators help keep your code clean and simple when working with big data.
Summary
Generators create values one by one, saving memory.
They are useful for big data or infinite sequences.
Use yield to pause and resume functions easily.
Practice
(1/5)
1. Why are generators useful in PHP programming?
easy
A. They save memory by producing values one at a time.
B. They automatically speed up all code execution.
C. They replace all loops with simpler syntax.
D. They store all data in arrays before processing.
Solution
Step 1: Understand what generators do
Generators produce values one by one instead of all at once, which saves memory.
Step 2: Compare with other options
Options A, B, and C are incorrect: A stores all data in arrays before processing, B does not automatically speed up code, C does not replace all loops.
Final Answer:
They save memory by producing values one at a time. -> Option A
Quick Check:
Generators save memory = D [OK]
Hint: Generators produce values lazily, saving memory [OK]
Common Mistakes:
Thinking generators speed up all code automatically
Confusing generators with arrays storing all data
Believing generators replace all loops
2. Which of the following is the correct syntax to create a generator function in PHP?
easy
A. function gen() { echo 1; }
B. function gen() { return 1; }
C. function gen() { yield 1; }
D. function gen() { print 1; }
Solution
Step 1: Identify generator syntax
Generators use the yield keyword to produce values one at a time.
Step 2: Check each option
function gen() { yield 1; } uses yield, so it is correct. Options B, C, and D do not use yield and are normal functions.
Final Answer:
function gen() { yield 1; } -> Option C
Quick Check:
yield keyword = A [OK]
Hint: Look for the 'yield' keyword to identify generators [OK]
Common Mistakes:
Using return instead of yield in generator functions