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

The `never` type is a bottom return type introduced in PHP 8.1 that indicates a function or method will never complete its normal execution cycle. When a function is declared with the `never` return type, it guarantees to the PHP engine and static analyzers that the program flow will halt, throw an exception, or enter an infinite loop before reaching the implicit return at the end of the function scope.

```php theme={"dark"}
function terminateExecution(): never {
    exit();
}
```

## Execution Mechanics and Enforcement

To satisfy the `never` type constraint, the function must terminate the current execution context via one of the following mechanisms:

1. Invoking `exit()` or `die()`.
2. Throwing an `Exception` or `Error`.
3. Executing an infinite loop (e.g., `while (true) {}`).

The PHP engine enforces the `never` constraint using two distinct mechanisms depending on how the rule is violated:

* **Compile-Time Enforcement:** Any explicit `return` statement inside a `never`-returning function (including a bare `return;` or returning a value) is caught during compilation and results in a `Fatal error`.
* **Runtime Enforcement:** If the function execution reaches the closing brace (an implicit return), PHP throws a `TypeError` at runtime.

```php theme={"dark"}
// Compile-time Fatal error: A never-returning function must not return
function explicitReturn(): never {
    return;
}

// Runtime TypeError: never-returning function must not implicitly return
function implicitReturn(): never {
    $x = 1 + 1;
}
```

## Type Theory and Subtyping

In type theory, `never` represents the empty set of values, making it the **bottom type**. Consequently, `never` is a subtype of every other type in PHP's type system, including `void` and `mixed`.

Because of return type covariance, a subclass method can narrow a parent class's return type to `never` regardless of the parent's original return type declaration.

```php theme={"dark"}
class Base {
    public function execute(): int {
        return 42;
    }
}

class Child extends Base {
    // Valid: 'never' is a subtype of 'int'
    public function execute(): never {
        throw new LogicException();
    }
}
```

Conversely, due to contravariance rules, a parent method returning `never` cannot be overridden by a method returning any other type, as there is no type narrower than `never`.

## Structural Restrictions

The PHP engine enforces strict rules regarding the placement and combination of the `never` type:

* **Return Type Exclusive:** It can only be used as a return type. It is strictly prohibited as a parameter type, property type, or class constant type.
* **Standalone Type:** It cannot be combined in a union or intersection type. Declarations such as `never|string` or `never&Countable` will result in a compile-time fatal error.

```php theme={"dark"}
// Fatal error: never cannot be used as a parameter type
function process(never $value): void {}

// Fatal error: never cannot be used in a union type
function fetch(): never|array {}
```

## Distinction from `void`

While both `void` and `never` indicate the absence of a returned value, their control flow guarantees differ fundamentally. A `void` function completes its execution and returns control to the calling scope (implicitly returning `null`). A `never` function strictly forbids control from ever returning to the calling scope.

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