A generator function is a special function that can pause its execution and return values one at a time using the yield keyword, instead of returning all values at once.
Click to reveal answer
beginner
How does the yield keyword affect the execution of a generator function?
The yield keyword pauses the function's execution and sends a value back to the caller. When the generator resumes, it continues from where it left off.
Click to reveal answer
intermediate
What happens when a generator function is called in PHP?
Calling a generator function returns a Generator object immediately without running the function body. The function body runs only when iterating over the generator.
Click to reveal answer
intermediate
Explain the difference between return and yield in a generator function.
return ends the generator and optionally sends a final value. yield pauses the generator and sends a value but allows resuming later to produce more values.
Click to reveal answer
beginner
What is the benefit of using a generator function over returning an array?
Generators use less memory because they produce values one at a time on demand, instead of creating and storing the entire array in memory.
Click to reveal answer
What does a generator function return when called in PHP?
AA Generator object
BAn array of values
CThe first yielded value
DNull
✗ Incorrect
Calling a generator function returns a Generator object immediately without running the function body.
Which keyword is used to pause a generator function and send a value back?
Ayield
Breturn
Cbreak
Dcontinue
✗ Incorrect
The yield keyword pauses the generator and sends a value back to the caller.
What happens when the generator resumes after a yield?
AIt restarts from the beginning
BIt ends immediately
CIt continues execution from the next statement after yield
DIt throws an error
✗ Incorrect
The generator resumes execution from the statement immediately after the yield.
Which of these is a key advantage of generators?
AFaster execution than normal functions always
BLower memory usage by producing values on demand
CThey can only return one value
DThey automatically cache all results
✗ Incorrect
Generators produce values one at a time, saving memory compared to building large arrays.
What does the return statement do inside a generator function?
ASkips the next yield
BPauses the generator and sends a value
CRestarts the generator
DEnds the generator and optionally sends a final value
✗ Incorrect
Return ends the generator function and can send a final value to the caller.
Describe how a generator function executes in PHP from the moment it is called until it finishes.
Think about the lifecycle of the generator and how yield controls execution.
You got /5 concepts.
Explain why using a generator function can be more memory efficient than returning an array.
Compare storing all data at once versus producing it step-by-step.
You got /3 concepts.
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?