📖 Scenario: Imagine you have a huge list of numbers, but you want to process them one by one without using too much memory.Generators help you do this by giving you one number at a time instead of all at once.
🎯 Goal: You will create a generator function in PHP that yields numbers from 1 to 5 one by one.This shows how generators save memory by not storing all numbers at once.
📋 What You'll Learn
Create a generator function called numberGenerator that yields numbers 1 to 5
Create a variable called gen that calls numberGenerator()
Use a foreach loop with variable $num to get values from gen
Print each number inside the loop using echo
💡 Why This Matters
🌍 Real World
Generators are useful when processing large files, streams, or data sets where loading everything at once is not possible or efficient.
💼 Career
Understanding generators helps in writing efficient PHP code for web applications, APIs, and data processing tasks.
Progress0 / 4 steps
1
Create the generator function
Create a generator function called numberGenerator that yields numbers 1, 2, 3, 4, and 5 using yield.
PHP
Hint
Use the yield keyword inside the function to return one number at a time.
2
Create the generator variable
Create a variable called gen and set it to the result of calling numberGenerator().
PHP
Hint
Call the function numberGenerator() and assign it to gen.
3
Use a foreach loop to get values
Use a foreach loop with variable $num to iterate over gen and get each number.
PHP
Hint
Use foreach ($gen as $num) to get each yielded value.
4
Print each number
Inside the foreach loop, print each number using echo followed by a space.
PHP
Hint
Use echo $num . " " to print each number with a space.
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