> ## 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 Return Statement

The `return` statement is a language construct that immediately terminates the execution of the current function, method, or script, and passes control—along with an optional evaluated value—back to the calling environment.

## Syntax

```php theme={"dark"}
return;
// or
return expression;
```

Because `return` is a language construct and not a function, parentheses around the evaluated expression are not required. Their use is generally discouraged in PHP, as it unnecessarily evaluates the expression within parentheses and misrepresents the construct as a function call.

```php theme={"dark"}
return $value;   // Preferred
return($value);  // Discouraged
```

## Execution Mechanics

* **Termination:** When the PHP parser encounters a `return` statement, it halts execution of the current scope block. Any code physically located after the `return` statement within that same execution path is unreachable.
* **Evaluation:** The `expression` is evaluated before being passed back to the caller.
* **Implicit Returns:** If the `return` statement is omitted from a function, or if it is called without an expression (i.e., `return;`), PHP implicitly returns `null`.
* **Try/Catch/Finally Execution:** If a `return` statement is encountered inside a `try` or `catch` block, the `finally` block will still execute *before* control and the evaluated value are actually passed back to the caller.

## Scope Contexts

The behavior of `return` changes depending on the execution context:

1. **Function/Method Scope:** Terminates the function and yields the evaluated expression to the caller.
2. **Global Scope:** Terminates the execution of the main script.
3. **Included/Required Files:** If executed within a file loaded via `include` or `require`, execution of that specific file halts. Control returns to the parent script, and the `return` value becomes the evaluated result of the `include`/`require` call itself.

## Return Type Declarations

PHP allows strict enforcement of the returned value's data type via function signatures. If a return type is declared, the evaluated expression must match this type (or be coercible to it, depending on `strict_types` configuration).

```php theme={"dark"}
function getInteger(): int {
    return 42; // The expression must evaluate to an integer
}

function getVoid(): void {
    return; // Must not return a value; omitting 'return' is also valid
}
```

## Returning by Reference

By default, PHP returns values by value (creating a copy). To return a variable by reference—allowing the caller to modify the original variable's memory address—the function declaration must be prefixed with an ampersand (`&`), and the assignment at the call site must use the reference operator (`=&`). The `return` statement itself does *not* use an ampersand.

```php theme={"dark"}
function &returnByReference(&$variable) {
    // The return statement uses standard syntax; no ampersand is used here
    return $variable; 
}

$myVar = 10;
// The ampersand is required at the call site during assignment
$ref =& returnByReference($myVar);
```

## Compound Returns

PHP does not natively support returning multiple discrete variables. The mechanical workaround is to return a single compound data structure (such as an array or an object) and utilize symmetric array destructuring at the call site.

```php theme={"dark"}
function returnCompound(): array {
    return ['first_value', 'second_value'];
}

// Destructuring the returned array into separate variables
[$a, $b] = returnCompound();
```

<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>
