exit()
The exit() function in PHP is used to immediately terminate the current script and return a specified exit code. It can be useful in certain situations where
What the exit() Function Does
The exit() function in PHP stops the current script right where it is called. No more code runs after it — the script ends immediately. It is most often used to halt execution after a redirect, when a fatal condition is detected, or to print a final message before quitting.
exit is a language construct, not a regular function, so the parentheses are optional: exit; and exit(); both work. die() is an exact alias of exit() — they behave identically, and which one you use is purely a matter of style. See the die() chapter for more on the alias.
This page covers what exit() does, the difference between its status and message arguments, the exit codes it returns to the shell, and the common mistakes to avoid.
Syntax and the Optional Argument
exit(int|string $status = 0): neverexit() accepts a single optional argument that can be either an integer or a string, and the two are treated very differently:
- Integer — used as the process exit status (0–254). It is not printed. By convention,
0means success and any non-zero value signals an error. This matters when a script is run from the command line and another program inspects its exit code. - String — printed just before the script ends, then the exit status is
0. This is handy for showing a final error message.
If you call exit() with no argument, the script ends with status 0 and prints nothing.
Printing a Message Before Quitting
When you pass a string, PHP outputs it and then stops:
<?php
$config = @file_get_contents('config.ini');
if ($config === false) {
exit("Fatal: config.ini could not be read.\n");
}
echo "Config loaded.\n";If the file is missing, the script prints Fatal: config.ini could not be read. and the final echo never runs.
Returning an Exit Code to the Shell
When you pass an integer, nothing is printed but the value becomes the process exit code — useful for scripts run from the terminal or a CI pipeline:
<?php
$ok = false; // pretend a check failed
if (!$ok) {
exit(1); // non-zero = failure
}
exit(0); // successRunning this with php script.php and then echo $? in the shell would print 1. A calling program can branch on that code without parsing any output.
Stopping a Script After a Redirect
The classic web use case is to guarantee no further code runs after sending a Location header:
<?php
if (!isset($_SESSION['user'])) {
header('Location: login.php');
exit();
}
// Protected code below only runs for logged-in users.Without the exit(), PHP would keep executing the lines after the redirect — potentially leaking protected output to a user who should have been sent to the login page. See header() and PHP Sessions for the pieces this snippet relies on.
Common Gotchas
exit()runs shutdown functions and object destructors, but it does not flush an output buffer started withob_start()if you exit mid-buffer in older setups — close buffers explicitly when it matters.- Status codes above 254 are reserved. Use
0–254;255is used internally by PHP. - Don't reach for
exit()as normal flow control. Inside functions, return a value or throw an exception so the caller can react. Anexit()buried in a function makes code hard to test, because it kills the whole process instead of just the call. - A string argument is not an error code.
exit("5")prints5and exits with status0, whileexit(5)exits with status5and prints nothing.
Summary
exit() (and its alias die()) ends a PHP script immediately. Pass an integer to set the shell exit status, or a string to print a final message. It is ideal for halting after a redirect or on an unrecoverable error, but for ordinary branching prefer if/else, return values, and exceptions so your code stays testable.