📖 Scenario: Imagine you want to process a list of numbers one by one without loading them all into memory at once. PHP generators help you do this efficiently.
🎯 Goal: You will create a generator function that yields numbers from 1 to 5, then use it to print each number step-by-step.
📋 What You'll Learn
Create a generator function named numberGenerator that yields numbers 1 through 5
Create a variable $gen that holds the generator returned by numberGenerator()
Use a foreach loop with variable $num to iterate over $gen
Print each number inside the loop using echo
💡 Why This Matters
🌍 Real World
Generators are useful when working with large data streams or files where loading everything at once is not efficient.
💼 Career
Understanding generators helps in writing memory-efficient PHP code, important for backend development and data processing tasks.
Progress0 / 4 steps
1
Create the generator function
Write a generator function called numberGenerator that yields the numbers 1, 2, 3, 4, and 5 one by one using yield.
PHP
Hint
Use the yield keyword inside the function to return each number one at a time.
2
Create the generator variable
Create a variable called $gen and assign it the generator returned by calling numberGenerator().
PHP
Hint
Call the function numberGenerator() and assign it to $gen.
3
Iterate over the generator
Use a foreach loop with variable $num to iterate over $gen.
PHP
Hint
Use foreach ($gen as $num) to get each number from the generator.
4
Print each number
Inside the foreach loop, print each number stored in $num followed by a newline using echo.
PHP
Hint
Use echo $num . "\n"; to print each number on its own line.
Practice
(1/5)
1. What does the yield keyword do inside a PHP generator function?
easy
A. It pauses the function and returns a value to the caller.
B. It immediately ends the function and returns a value.
C. It creates a new variable inside the function.
D. It calls another function asynchronously.
Solution
Step 1: Understand the role of yield in generators
The yield keyword pauses the function execution and sends a value back to the caller without ending the function.
Step 2: Compare with other options
Unlike return, yield does not end the function but allows it to resume later.
Final Answer:
It pauses the function and returns a value to the caller. -> Option A
Quick Check:
yield pauses and returns value [OK]
Hint: Remember: yield pauses, return ends function [OK]
Common Mistakes:
Confusing yield with return
Thinking yield creates variables
Assuming yield calls other functions
2. Which of the following is the correct syntax to define a generator function in PHP?
easy
A. function gen() { return yield 1; }
B. function gen() { yield 1; }
C. function gen() => yield 1;
D. generator gen() { yield 1; }
Solution
Step 1: Identify correct generator syntax
In PHP, a generator function is a normal function that uses yield inside its body.
Step 2: Check each option
function gen() { yield 1; } uses a normal function with yield correctly. function gen() { return yield 1; } misuses return yield. function gen() => yield 1; uses invalid arrow syntax. generator gen() { yield 1; } uses an invalid keyword generator.
Final Answer:
function gen() { yield 1; } -> Option B
Quick Check:
Normal function + yield = generator [OK]
Hint: Generator = function with yield inside braces [OK]
Common Mistakes:
Using return with yield incorrectly
Trying arrow function syntax for generators
Using non-existent keywords like 'generator'
3. What will be the output of the following PHP code?