die()
The die() function in PHP is used to print a message and terminate the current script execution. It is essentially the same as the exit() function, which is
What the die() Function Does
The die() function stops the PHP script right where it is called. Nothing after it runs — no further statements, no later output, no remaining loops. Optionally, it prints a message (or returns a status code) at the moment it halts.
die() is an alias of exit(): the two are byte-for-byte interchangeable, take the same argument, and behave identically. PHP keeps both names because die reads naturally in "stop on error" code (... or die(...)) while exit reads naturally when you simply want to end a script. Use whichever makes the surrounding code clearer; this page uses die() throughout.
Syntax
die(string $message = "")
die(int $status = 0)die() accepts one optional argument, and what you pass changes its behavior:
- A string — PHP prints that string, then terminates. This is the common case.
- An integer — PHP terminates with that value as the exit status and prints nothing.
0means success;1–254signal an error to the shell. This matters only for command-line scripts (php script.php), where another program reads$?to learn whether your script succeeded. - Nothing — the script just ends, with no output and an exit status of
0.
die() is a language construct, not a regular function, so the parentheses are optional (die; is valid). Including them keeps the code consistent with normal function calls.
Stopping a Script on a Failed Condition
The most frequent use of die() is to bail out the moment something is wrong, so the rest of the script never runs with bad data:
Because $name is not "Jane", the if body runs, die() prints Access denied!, and the script ends immediately — the echo line is never reached. If $name were "Jane", die() would be skipped and the script would print Welcome, Jane! instead.
The "or die()" Idiom
die() is often chained to an expression with or. PHP evaluates the left side first; only if it is falsy (such as false or null) does it move on and run die(). This is the classic way to guard an operation that can fail:
<?php
$file = fopen("missing-config.txt", "r") or die("Could not open the config file.");
echo "File opened successfully.";If fopen() succeeds it returns a file handle (truthy), so or die(...) is never reached. If the file is missing, fopen() returns false, die() runs, and the script stops before the echo.
This idiom is concise, but in modern code prefer real error handling — throwing an exception or logging through error_log() — so failures can be caught and reported rather than abruptly killing the request. See trigger_error() and error reporting for cleaner alternatives.
Exit Status for Command-Line Scripts
When you run PHP from the terminal, the exit status tells the calling shell or CI pipeline whether the script worked:
<?php
$ok = false;
if (!$ok) {
fwrite(STDERR, "Job failed\n"); // write the message yourself...
die(1); // ...then exit with a non-zero status
}
die(0); // successPassing an integer to die() sets the status but prints nothing, so write any human-readable message yourself (here to STDERR) before exiting. A surrounding script can then check $? and react to the 1.
Gotchas
die()skips no cleanup you registered withregister_shutdown_function()— shutdown callbacks and object destructors still run. But code written after thedie()call never executes.- In a web request,
die()ends the whole response. Output already sent to the browser stays; everything queued after the call is dropped. Avoid it inside functions meant to be reusable — return a value or throw instead. - An integer argument prints nothing.
die(1)does not display1; if you want the user to see a message, pass a string or print it before callingdie(). - Prefer exceptions in application code.
die()cannot be caught, tested easily, or recovered from. Reserve it for top-level scripts and quick diagnostics.
Conclusion
die() (and its twin exit()) terminates a PHP script on the spot, optionally printing a message or setting an exit status. It shines for fast guards in standalone and command-line scripts, but for production application logic, lean on exceptions and proper error handling so failures stay catchable. To go deeper on writing and structuring code that calls it, see PHP functions.