ZetCode

PHP never Keyword

last modified November 25, 2026

The PHP never return type, introduced in PHP 8.1, declares that a function does not return at all. It either throws an exception or ends the script, and it makes that guarantee visible to readers and static analysis tools.

This article compares never with void, shows the valid ways to leave a never function, and demonstrates the errors PHP reports for invalid declarations and for a function that returns implicitly.

Basic definitions

The never return type, introduced in PHP 8.1, marks a function that does not return to its caller. Such a function either throws an exception or ends the script with exit. It is the bottom type of PHP's type system: it is a subtype of every other type, although no value of that type can ever exist.

never may be used only as a return type and only on its own. Nullable or union forms such as ?never and never|string are rejected, and so is a parameter or property of type never. The function body must not return: an explicit return; is a compile-time error, and falling off the end of the function is a TypeError at run time.

never is stricter than void. A void function returns control to the caller without a value; a never function never gives control back. The type documents the behaviour for readers and lets static analysis mark the code after the call as unreachable.

A function that throws

The first example declares a function that always throws. The exception leaves the function, so control never reaches the statement after the call inside it.

never_throw.php
<?php

declare(strict_types=1);

function fail(string $message): never
{
    throw new RuntimeException($message);
}

try {
    fail("Cannot open the configuration file");
} catch (RuntimeException $e) {
    echo "Caught: ", $e->getMessage(), "\n";
}

echo "The script continues\n";

The call to fail raises RuntimeException, which propagates to the catch block in the caller. The script then continues, because the exception was handled outside the function. The never declaration says that fail itself produces no return value; it does not forbid the caller from catching the exception.

$ php never_throw.php
Caught: Cannot open the configuration file
The script continues

void versus never

The difference between the two return types is easiest to see when both appear side by side.

never_void.php
<?php

declare(strict_types=1);

function logMessage(string $message): void
{
    echo "log: $message\n";
}

function fail(string $message): never
{
    throw new RuntimeException($message);
}

logMessage("service started");

try {
    fail("service stopped");
} catch (RuntimeException $e) {
    echo "caught: ", $e->getMessage(), "\n";
}

echo "done\n";

logMessage is declared void: it prints a line and then returns normally. fail is declared never: it can only leave through the exception. Neither function returns a value, but only the second one guarantees that the next statement in the caller is never reached through a normal return. Use never for functions whose whole purpose is to abort, and void for ordinary side effects.

$ php never_void.php
log: service started
caught: service stopped
done

Ending the script with exit

A function that calls exit also never returns. The integer passed to exit becomes the process exit status.

never_exit.php
<?php

declare(strict_types=1);

function shutdown(int $code): never
{
    echo "Shutting down with status $code\n";
    exit($code);
}

shutdown(3);

echo "This line is never reached\n";

The call prints one line and terminates the process with status 3, so the final echo is never executed. From the shell, echo $? after the script would print 3. A never function may use exit or die; both satisfy the return type because control does not come back.

$ php never_exit.php
Shutting down with status 3

A helper for match

A never function is a natural fit for the default arm of a match expression. The arm has to produce a string, and a function that always throws is accepted in that position because never is compatible with every type.

never_match.php
<?php

declare(strict_types=1);

function abort(string $message): never
{
    throw new LogicException($message);
}

function label(int $code): string
{
    return match ($code) {
        200 => "OK",
        404 => "Not found",
        default => abort("Unknown status $code"),
    };
}

echo label(200), "\n";

try {
    label(500);
} catch (LogicException $e) {
    echo "Error: ", $e->getMessage(), "\n";
}

abort always throws LogicException, so label always returns a string or leaves through the exception. The default arm is type-correct without a dummy string value. Writing default => throw new LogicException(...) directly is also valid; the helper keeps the message construction in one place and gives the abort a name.

$ php never_match.php
OK
Error: Unknown status 500

An implicit return is an error

Declaring never does not force PHP to prove at compile time that the body never returns. The check happens when the function actually returns, and the result is a TypeError.

never_implicit.php
<?php

declare(strict_types=1);

function announce(): never
{
    echo "announced\n";
}

try {
    announce();
} catch (TypeError $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

The function prints its message and then reaches the closing brace, which is an implicit return. PHP raises TypeError: announce(): never-returning function must not implicitly return. The exception is thrown at the boundary of the function, so the caller can catch it, although a correct program should never produce it.

$ php never_implicit.php
announced
TypeError: announce(): never-returning function must not implicitly return

Declarations PHP rejects

Because never is a bottom type, combining it with other types is meaningless, and PHP rejects such declarations while compiling the file.

never_invalid.php
<?php

declare(strict_types=1);

function stop(): ?never
{
}

The message is never can only be used as a standalone type. A parameter of type never reports never cannot be used as a parameter type, and a property reports Property A::$x cannot have type never. None of these declarations can be made valid by adding ? or a union; the type has exactly one legal position, the return type.

$ php never_invalid.php
Fatal error: never can only be used as a standalone type in /app/never_invalid.php on line 5
Stack trace:
#0 {main}

Best practices

Use the type only where it describes the contract truthfully, and keep ordinary returns in void functions.

Source

PHP manual: never

PHP manual: return type declarations

This article covered the PHP never return type: throwing functions, termination with exit, the difference from void, the runtime error for an implicit return, and the declarations PHP rejects. Continue with PHP void keyword for the ordinary no-value return type and PHP throw keyword for exceptions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.