exit

Ends script execution immediately, printing a message or returning a status code to the caller.

Signature
exit(string|int $status = 0): never
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$statusoptionalstring|int= 0A string is printed before the script ends and the exit code stays 0. An integer becomes the process exit code. Use 0-254; 255 is reserved by PHP.

Return value

neverNever returns; the script terminates.

Errors

Does not throw for valid values. As of PHP 8.4 exit() follows the usual type juggling semantics, so an invalid value such as an array, object or resource raises a TypeError even in weak mode (verified: exit([]) throws "exit(): Argument #1 ($status) must be of type string|int, array given"); under strict_types=1 any argument that is not a string or int, floats and bools included, also raises a TypeError.

Changelog

  • PHP 8.4.0exit() is a proper function: it obeys standard type juggling and strict_types, supports named arguments, and can be used as a variable function or callable.
  • PHP 8.4.0A parameterless exit() called from a shutdown function or destructor resets the exit code to 0 rather than preserving the previously set code.

exit stops the script on the spot. It can be written with or without parentheses (exit;, exit();, exit(1);), and die is an exact alias.

Message or status code

The single argument does double duty. A string is printed to output and the exit code stays 0. An integer is not printed and becomes the process exit code the shell sees. That is the distinction most people get wrong: exit("failed") reports success to the caller.

PHP 8.4 made it a function

Before 8.4, exit was a language construct, so it could not be used as a callable and did not follow type juggling rules. In PHP 8.4 and later it is a genuine function: array_map(exit(...), ...)-style usage compiles, named arguments work, and under strict_types=1 passing a float or bool is a TypeError instead of being silently coerced.

What trips people up

Shutdown functions and destructors still run after exit, but finally blocks never do. In long-lived contexts, exit kills the whole request, so in library code prefer throwing an exception and let the caller decide.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Returning a nonzero status from a CLI script so a cron job or CI step registers the failure.
  • Ending the request right after sending a Location header so no page body follows the redirect.
  • Stopping a bootstrap script when a required extension or configuration value is missing.

Frequently asked questions

What is the difference between exit and return?

return leaves the current function and lets the caller continue; exit terminates the entire script. Use return in reusable code and reserve exit for the top level of a script.

Does exit run the finally block?

No. finally blocks are skipped by exit, though destructors and functions registered with register_shutdown_function() still run.

What exit code does PHP use by default?

0, meaning success. Pass an integer between 1 and 254 to signal an error; 255 is reserved by PHP itself.

More examples

String prints, integer sets the code

Only the echoed lines and "cleanup done" appear; the integer 2 is never printed because it is used as the process exit status instead.

Example · php
<?php
register_shutdown_function(function () {
    echo "cleanup done\n";
});

echo "string form prints its argument\n";
$code = 2;
echo "now exiting with code {$code}\n";
exit($code);

exit as a callable (PHP 8.4+)

Since PHP 8.4 exit is a real function, so the first-class callable syntax exit(...) produces a Closure that terminates the script when invoked — impossible before 8.4.

Example · php
<?php
register_shutdown_function(fn () => print("bye\n"));

$stop = exit(...);

echo "about to call a first-class exit\n";
$stop("stopped via callable\n");

Discussion

  • Be the first to comment on this lesson.