> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP Yield From Expression

The `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.

```php theme={"dark"}
yield from <iterable>;
```

## Execution Mechanics

When the PHP engine evaluates a `yield from` expression, it temporarily suspends the execution of the current generator and transfers control to the provided `<iterable>`.

1. **Value Emission:** Every value and key produced by the sub-iterable is passed directly to the outer generator's caller.
2. **Key Preservation:** `yield from` strictly preserves the keys yielded by the delegated iterable. If an array with specific string or integer keys is passed to `yield from`, those exact keys are emitted to the caller.
3. **Bidirectional Communication:** If the delegated iterable is another generator, `yield from` establishes a transparent conduit. Calls to `Generator::send()` or `Generator::throw()` on the parent generator are forwarded directly to the active sub-generator.

## Expression Evaluation and Return Values

Both `yield` 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 `return` statement, the `yield from` expression evaluates to that returned value.
* **Arrays and Traversables:** If the delegated iterable is an array or a standard `Traversable` object, the `yield from` expression evaluates to `null`.

```php theme={"dark"}
function subGenerator(): Generator {
    yield 'A';
    yield 'B';
    return 'Sub-generator execution finished';
}

function parentGenerator(): Generator {
    // Delegates yielding to an array. Evaluates to null.
    yield from [1, 2]; 

    // Delegates yielding to another generator. Evaluates to the generator's return value.
    $returnValue = yield from subGenerator(); 
    
    yield $returnValue;
}

// Iteration output: 1, 2, 'A', 'B', 'Sub-generator execution finished'
```

## Syntax Rules

* `yield from` can only be used within a function, method, or closure, which implicitly marks that callable as a `Generator`.
* It accepts exactly one expression that must evaluate to an `iterable` type. Passing a non-iterable scalar or object will result in a fatal `Error` with 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 the `yield from` expression is evaluated within larger operations (e.g., `$result = 5 + (yield from $iterable);`).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
