Recall & Review
beginner
What does the
yield keyword do in PHP?The
yield keyword allows a function to return a value to the caller without losing its state, enabling the function to be resumed later to produce a sequence of values one at a time.Click to reveal answer
beginner
How is
yield different from return in PHP?return ends the function and sends a single value back, while yield pauses the function, sends a value back, and allows the function to continue from where it left off when called again.Click to reveal answer
intermediate
What type of object does a function using
yield return in PHP?It returns a
Generator object, which can be iterated over to get each yielded value one by one.Click to reveal answer
intermediate
Can
yield send keys along with values? How?Yes, you can use
yield key => value; to send both a key and a value, which is useful when iterating over the generator with keys.Click to reveal answer
intermediate
Why use
yield instead of building an array in PHP?Using
yield saves memory because it generates values one at a time instead of creating a full array in memory, which is helpful for large data sets or infinite sequences.Click to reveal answer
What does the
yield keyword return when used inside a PHP function?✗ Incorrect
yield returns a Generator object that can be iterated to get each yielded value.Which statement about
yield is true?✗ Incorrect
yield pauses the function and returns a value, allowing the function to resume later.How do you send a key and value using
yield?✗ Incorrect
The syntax
yield key => value; sends both a key and a value.Why might you prefer
yield over returning an array?✗ Incorrect
yield generates values one at a time, saving memory compared to building a full array.What happens if you call a function with
yield multiple times?✗ Incorrect
Each call to the generator's next() method resumes the function and produces the next yielded value.
Explain how the
yield keyword works in PHP and how it differs from return.Think about how <code>yield</code> lets a function produce multiple values over time.
You got /5 concepts.
Describe a situation where using
yield is better than returning an array.Consider when you have many values but don't want to store them all at once.
You got /4 concepts.