PHP: Hypertext Preprocessor (original) (raw)
exit
(PHP 4, PHP 5, PHP 7, PHP 8)
exit — Terminate the current script with a status code or message
Description
Terminates execution of the script.Shutdown functions and object destructors will always be executed even if exit() is called. However, finally blocks are never executed.
An exit code of 0
is used to indicate that the program succeeded in its tasks. Any other value indicates some sort of error occurred during execution.
exit() is a special function, because it has a dedicated token in the parser, as such it can be used like a statement (i.e. without parentheses) to terminate the script with the default status code.
Caution
It is not possible to disable, or create a namespaced function shadowing the global exit() function.
Parameters
status
If status
is a string, this function prints the status
just before exiting. The exit code returned by PHP is 0
.
If status
is an int, the exit code returned by PHP will be status
.
Note: Exit codes should be in the range
0
to254
, the exit code255
is reserved by PHP and should not be used.
Return Values
As this terminates the PHP script, no value is returned.
Examples
Example #1 Basic exit() example
<?php// exit program normally exit(); exit(0);// exit with an error code exit(1);?>
Example #2 exit() example with a string
`<?php
$filename
= '/path/to/data-file'; file=fopen(file = fopen(file=fopen(filename, 'r')
or exit("unable to open file ($filename)");?>`
Example #3 Shutdown functions and destructors run regardless
`<?php
class Foo
{
public function __destruct()
{
echo 'Destruct: ' . METHOD . '()' . PHP_EOL;
}
}
function
shutdown()
{
echo 'Shutdown: ' . FUNCTION . '()' . PHP_EOL;
}$foo = new Foo();
register_shutdown_function('shutdown');
exit();
echo
'This will not be output.';
?>`
The above example will output:
Shutdown: shutdown() Destruct: Foo::__destruct()
Example #4 exit() as a statement
<?php// exit program normally with exit code 0 exit;?>
Notes
Warning
Prior to PHP 8.4.0, exit() was a language construct and not a function, therefore it was not possible to call it usingvariable functions, or named arguments.
See Also
- register_shutdown_function() - Register a function for execution on shutdown
- Shutdown functions
- object destructors
Found A Problem?
dexen dot devries at gmail dot com ¶
14 years ago
`` If you want to avoid calling exit() in FastCGI as per the comments below, but really, positively want to exit cleanly from nested function call or include, consider doing it the Python way:
define an exception named `SystemExit', throw it instead of calling exit() and catch it in index.php with an empty handler to finish script execution cleanly.
``
albert at removethis dot peschar dot net ¶
15 years ago
`` jbezorg at gmail proposed the following:
After sending the `Location:' header PHP will continue parsing, and all code below the header() call will still be executed. So instead use:
``
10 years ago
`A side-note for the use of exit with finally: if you exit somewhere in a try block, the finally won't be executed. Could not sound obvious: for instance in Java you never issue an exit, at least a return in your controller; in PHP instead you could find yourself exiting from a controller method (e.g. in case you issue a redirect).
Here follows the POC:
This will print:
testing finally wit exit
In try, exiting
`
tianyiw at vip dot qq dot com ¶
2 years ago
`These are the standard error codes in Linux or UNIX.
1 - Catchall for general errors
2 - Misuse of shell builtins (according to Bash documentation)
126 - Command invoked cannot execute
127 - “command not found”
128 - Invalid argument to exit
128+n - Fatal error signal “n”
130 - Script terminated by Control-C
255* - Exit status out of range
`
vincent dot laag at gmail dot com ¶
14 years ago
Don't use the exit() function in the auto prepend file with fastcgi (linux/bsd os). It has the effect of leaving opened files with for result at least a nice "Too many open files ..." error.
7 years ago
`Calling 'exit' will bypass the auto_append_file option.
On some free hosting this risks you getting removed, as they may be using for ads and analytics.
So be a bit careful if using this on the most common output branch.
`
21 years ago
Note, that using exit() will explicitly cause Roxen webserver to die, if PHP is used as Roxen SAPI module. There is no known workaround for that, except not to use exit(). CGI versions of PHP are not affected.
void a t informance d o t info ¶
16 years ago
`To rich dot lovely at klikzltd dot co dot uk:
Using a "@" before header() to suppress its error, and relying on the "headers already sent" error seems to me a very bad idea while building any serious website.
This is not a clean way to prevent a file from being called directly. At least this is not a secure method, as you rely on the presence of an exception sent by the parser at runtime.
I recommend using a more common way as defining a constant or assigning a variable with any value, and checking for its presence in the included script, like:
in index.php:
in your included file:
BR.
Ninj
`
m dot libergolis at gmail dot com ¶
9 years ago
`In addition to "void a t informance d o t info", here's a one-liner that requires no constant:
Placing it at the beginning of a PHP file will prevent direct access to the script.
To redirect to / instead of dying:
Doing the same in a one-liner:
A note to security: Even though $_SERVER['PHP_SELF'] comes from the user, it's safe to assume its validity, as the "manipulation" takes place before the actual file execution, meaning that the string must have been valid enough to execute the file. Also, basename() is binary safe, so you can safely rely on this function.
`
13 years ago
`When using php-fpm, fastcgi_finish_request() should be used instead of register_shutdown_function() and exit()
For example, under nginx and php-fpm 5.3+, this will make browsers wait 10 seconds to show output:
"; register_shutdown_function('shutdown'); exit; function shutdown(){ sleep(10); echo "Because exit() doesn't terminate php-fpm calls immediately."; } ?>
This doesn't:
"; fastcgi_finish_request(); sleep(10); echo "You can't see this form the browser."; ?>`
devinemke at devinemke dot com ¶
23 years ago
`If you are using templates with numerous includes then exit() will end you script and your template will not complete (no , , etc...). Rather than having complex nested conditional logic within your content, just create a "footer.php" file that closes all of your HTML and if you want to exit out of a script just include() the footer before you exit().
for example:
include ('header.php');
blah blah blah
if (!$mysql_connect) {
echo "unable to connect";
include ('footer.php');
exit;
}
blah blah blah
include ('footer.php');
`
7 years ago
`>> Shutdown functions and object destructors will always be executed even if exit is called.
It is false if you call exit into desctructor.
Normal exit:
<?php
class A
{
public function __destruct()
{
echo "bye A\n";
}
}
class
B
{
public function __destruct()
{
echo "bye B\n";
}
}$a = new A;
$b = new B;
exit;// Output:
// bye B
// bye A
?>
// Exit into desctructor:
<?php
class A
{
public function __destruct()
{
echo "bye A\n";
}
}
class
B
{
public function __destruct()
{
echo "bye B\n";
exit;
}
}$a = new A;
$b = new B;// Output:
// bye B
?>
`