0
0
PHPprogramming~5 mins

Yield keyword behavior in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA Generator object
BAn array
CA string
DA boolean
Which statement about yield is true?
A<code>yield</code> pauses the function and returns a value
B<code>yield</code> immediately ends the function
C<code>yield</code> returns all values at once
D<code>yield</code> is used to declare variables
How do you send a key and value using yield?
Ayield value => key;
Byield (key, value);
Cyield key => value;
Dyield key, value;
Why might you prefer yield over returning an array?
AIt converts values to strings
BIt runs faster because it returns all values immediately
CIt automatically sorts the values
DIt uses less memory by generating values one at a time
What happens if you call a function with yield multiple times?
AIt returns the last yielded value only
BIt produces a sequence of values, one per call to the generator
CIt throws an error
DIt returns an array of all yielded values
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.