Recall & Review
beginner
What is a generator in PHP?
A generator is a special function that can pause and resume its execution, yielding values one at a time instead of returning them all at once.
Click to reveal answer
beginner
Why are generators useful for memory management?
Generators produce values one by one, so they don't need to store the entire data set in memory, saving memory especially with large data.
Click to reveal answer
intermediate
How do generators improve performance compared to returning arrays?
Generators start producing values immediately and don’t wait to build the whole array, so they can be faster and more efficient for large or infinite sequences.
Click to reveal answer
beginner
What keyword is used in PHP to yield values from a generator?
The
yield keyword is used to return a value from a generator and pause its execution until the next value is requested.Click to reveal answer
beginner
Give a real-life analogy for how generators work.
Generators are like a vending machine that gives you one snack at a time when you press a button, instead of giving you the whole box at once.
Click to reveal answer
What is the main advantage of using generators in PHP?
✗ Incorrect
Generators yield values one at a time, which saves memory compared to storing all values in an array.
Which keyword is used to produce values inside a PHP generator?
✗ Incorrect
The
yield keyword is used to return values from a generator function.When would you prefer a generator over returning an array?
✗ Incorrect
Generators are ideal for large data sets because they yield values one by one, reducing memory use.
What happens when a generator function reaches a yield statement?
✗ Incorrect
At a yield, the generator returns the current value and pauses until the next value is requested.
Which of these is NOT a benefit of generators?
✗ Incorrect
Generators do not automatically provide parallel processing; they help with memory and performance but not concurrency.
Explain why generators are needed in PHP and how they help with memory and performance.
Think about how returning all data at once can be costly.
You got /3 concepts.
Describe how the
yield keyword works inside a generator function.Compare it to a pause button in a video.
You got /3 concepts.