TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
yield from expression delegates the execution of a generator to another iterable structure, such as an array, a Traversable object, or another generator. It allows a parent generator to yield all values from a sub-iterable directly to the calling context without requiring an explicit foreach loop to extract and re-yield those values.
Execution Mechanics
When the PHP engine evaluates ayield from expression, it temporarily suspends the execution of the current generator and transfers control to the provided <iterable>.
- Value Emission: Every value and key produced by the sub-iterable is passed directly to the outer generator’s caller.
- Key Preservation:
yield fromstrictly preserves the keys yielded by the delegated iterable. If an array with specific string or integer keys is passed toyield from, those exact keys are emitted to the caller. - Bidirectional Communication: If the delegated iterable is another generator,
yield fromestablishes a transparent conduit. Calls toGenerator::send()orGenerator::throw()on the parent generator are forwarded directly to the active sub-generator.
Expression Evaluation and Return Values
Bothyield and yield from are expressions. However, they evaluate to different values based on their mechanics. While a standard yield expression evaluates to the value passed back to the generator via Generator::send(), a yield from expression evaluates to the return value of the delegated iterable once it is completely exhausted.
- Generators: If the delegated iterable is a generator that terminates with a
returnstatement, theyield fromexpression evaluates to that returned value. - Arrays and Traversables: If the delegated iterable is an array or a standard
Traversableobject, theyield fromexpression evaluates tonull.
Syntax Rules
yield fromcan only be used within a function, method, or closure, which implicitly marks that callable as aGenerator.- It accepts exactly one expression that must evaluate to an
iterabletype. Passing a non-iterable scalar or object will result in a fatalErrorwith the message “Can use ‘yield from’ only with arrays and Traversables”. - Because it is an expression, its result can be assigned directly to a variable (e.g.,
$result = yield from $iterable;). Parentheses are not required for simple assignment, but they are strictly required when theyield fromexpression is evaluated within larger operations (e.g.,$result = 5 + (yield from $iterable);).
Master PHP with Deep Grasping Methodology!Learn More





