The yield keyword helps create simple functions that produce values one at a time, saving memory and making code easier to read.
Yield keyword behavior in PHP
<?php function generatorFunction() { yield 1; }
yield pauses the function and sends a value back to the caller.
Each time the generator is called again, it resumes right after the last yield.
<?php function countToThree() { yield 1; yield 2; yield 3; }
<?php function letters() { yield 'a'; yield 'b'; yield 'c'; }
$max.<?php function squares($max) { for ($i = 1; $i <= $max; $i++) { yield $i * $i; } }
This program uses a generator to yield two words, printing each on its own line.
<?php function simpleGenerator() { yield 'Hello'; yield 'World'; } foreach (simpleGenerator() as $word) { echo $word . "\n"; }
Generators created with yield are memory efficient because they don't build the whole list at once.
You can use foreach to loop over values yielded by the generator.
Generators can also receive values back using yield with keys or by sending values, but that is more advanced.
Yield lets functions produce values one at a time.
It helps save memory and write cleaner code for sequences.
Use foreach to get each yielded value step-by-step.
Practice
yield keyword do in a PHP function?Solution
Step 1: Understand yield behavior
Theyieldkeyword pauses the function and returns a value without ending it.Step 2: Compare with return
Unlikereturn,yieldallows 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 AQuick Check:
Yield pauses and resumes function = A [OK]
- Confusing yield with return which ends function
- Thinking yield creates variables
- Assuming yield converts function to class method
Solution
Step 1: Recall yield syntax
The correct syntax to yield a value is simplyyield $value;inside the function.Step 2: Check other options
Options B, C, and D use invalid or incorrect syntax for yield in PHP.Final Answer:
yield $value; -> Option DQuick Check:
Simple yield syntax is 'yield $value;' [OK]
- Adding return before yield
- Using arrow (=>) with yield
- Calling yield like a function
function gen() {
yield 1;
yield 2;
yield 3;
}
foreach (gen() as $val) {
echo $val . ' ';
}Solution
Step 1: Understand generator yields
The functiongen()yields values 1, 2, and 3 one by one.Step 2: foreach prints each yielded value
Theforeachloop prints each value followed by a space, so output is '1 2 3 '.Final Answer:
1 2 3 -> Option AQuick Check:
Yielded values printed with spaces = B [OK]
- Expecting concatenated string without spaces
- Thinking yield returns an array
- Assuming syntax error with yield
function numbers() {
yield 1
yield 2;
}
foreach(numbers() as $n) {
echo $n;
}Solution
Step 1: Check yield statements
The first yield statement is missing a semicolon at the end, causing a syntax error.Step 2: Validate other parts
Using yield inside a function and foreach syntax are correct; yield does not require parentheses.Final Answer:
Missing semicolon after first yield statement -> Option BQuick Check:
Each yield needs semicolon [OK]
- Forgetting semicolon after yield
- Thinking yield needs parentheses
- Assuming foreach syntax error
function filterPositive(array $numbers) {
foreach ($numbers as $num) {
if ($num > 0) {
yield $num;
}
}
}
$values = [-2, 0, 3, 5, -1];
foreach (filterPositive($values) as $val) {
echo $val . ',';
}What is the output?
Solution
Step 1: Analyze filtering condition
The function yields only numbers greater than 0, so it skips -2, 0, and -1.Step 2: Identify yielded values
Only 3 and 5 satisfy the condition and are yielded, printed with commas.Final Answer:
3,5, -> Option CQuick Check:
Yield filters positive numbers only = D [OK]
- Including zero or negative numbers
- Thinking yield can't be inside if
- Expecting all values printed
Ready to prove it?
Pass the test and this pattern moves unknown
