die
Terminates the script immediately, optionally printing a message or setting an exit code.
die(string|int $status = 0): never- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$statusoptional | string|int= 0 | A string is printed before terminating and the exit code becomes 0. An integer is used as the process exit code and is not printed. Codes 0-254 are valid; 255 is reserved by PHP. |
Return value
neverNever returns; execution of the script ends.
Errors
Does not throw for valid values. As of PHP 8.4 die/exit follows the usual type juggling semantics, so an invalid value such as an array, object or resource raises a TypeError even without strict_types (verified: die([]) throws "exit(): Argument #1 ($status) must be of type string|int, array given"); under strict_types=1 a float or bool argument raises a TypeError as well instead of being coerced.
Changelog
- PHP 8.4.0exit() (and therefore die) became a real function: it follows normal type juggling, honours strict_types, accepts named arguments and can be used as a callable.
- PHP 8.4.0A parameterless call inside a shutdown function or destructor now resets the exit code to 0 instead of keeping the code from the earlier call.
die is an alias of exit. They are the same thing — same parameter, same behaviour, same exit codes — and the choice between them is pure style. Many codebases use die() for error bail-outs and exit() for a normal end, but PHP makes no distinction.
What actually happens
Everything stops at that line. Shutdown functions registered with register_shutdown_function() still run, and object destructors still fire, so cleanup and logging registered that way are safe. finally blocks are not executed, which is the classic surprise: a die() inside try skips the finally that would have closed your handle.
What trips people up
A string argument is printed, not used as a status code, so die("error") exits with code 0 and the shell sees success. To signal failure to a caller you need an integer: die(1). In web code, dying mid-response leaves whatever was already sent in the browser, so it is a poor error handler compared with throwing an exception that a handler can render properly.
Example
When to use it
- Stopping a CLI script early after a fatal configuration problem, with a nonzero code for the calling shell.
- Ending a small script right after sending a redirect header so no further output is produced.
- Bailing out of a one-off maintenance script when a required file is missing.
Frequently asked questions
What is the difference between die and exit in PHP?
There is none. die is documented as an alias of exit, so both accept the same string or integer argument and behave identically.
Does die run finally blocks or destructors?
Destructors and shutdown functions do run, but finally blocks do not. Put cleanup that must happen in a destructor or register_shutdown_function().
How do I make die return a failing exit code?
Pass an integer, such as die(1). A string argument is printed and leaves the exit code at 0.
More examples
Shutdown functions still run, finally does not
The output is "in try", "dying now", then "shutdown handler ran". The finally block is skipped entirely, which is why cleanup belongs in a shutdown function or destructor rather than finally when die() is possible.
<?php
register_shutdown_function(function () {
echo "shutdown handler ran\n";
});
try {
echo "in try\n";
die("dying now\n");
} finally {
echo "this finally never runs\n";
}Message versus exit code
Passing an integer sets the process exit code and prints nothing; only the echoed lines appear, and the shell would report status 3 rather than 0.
<?php
register_shutdown_function(function () {
echo "exit code will be: ", (int) (connection_status() === 0 ? 3 : 0), "\n";
});
echo "about to exit with code 3\n";
die(3);
Discussion