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
✗ Incorrect
yield returns a Generator object that can be iterated to get each yielded value.
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
✗ 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?
Ayield value => key;
Byield (key, value);
Cyield key => value;
Dyield key, value;
✗ Incorrect
The syntax yield key => value; sends both a key and a 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
✗ 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?
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
✗ 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.
Practice
(1/5)
1. What does the yield keyword do in a PHP function?
easy
A. It returns a value and pauses the function, resuming later to produce more values.
B. It immediately stops the function and returns a single value.
C. It creates a new variable inside the function.
D. It converts the function into a class method automatically.
Solution
Step 1: Understand yield behavior
The yield keyword pauses the function and returns a value without ending it.
Step 2: Compare with return
Unlike return, yield allows the function to resume and produce more values later.
Final Answer:
It returns a value and pauses the function, resuming later to produce more values. -> Option A
Quick Check:
Yield pauses and resumes function = A [OK]
Hint: Yield pauses function to produce values one by one [OK]
Common Mistakes:
Confusing yield with return which ends function
Thinking yield creates variables
Assuming yield converts function to class method
2. Which of the following is the correct syntax to yield values from a function in PHP?
easy
A. yield($value);
B. return yield $value;
C. yield => $value;
D. yield $value;
Solution
Step 1: Recall yield syntax
The correct syntax to yield a value is simply yield $value; inside the function.
Step 2: Check other options
Options B, C, and D use invalid or incorrect syntax for yield in PHP.