Hướng dẫn stop php

[PHP 4, PHP 5, PHP 7, PHP 8]

exitOutput a message and terminate the current script

Description

exit[string $status = ?]: void

exit[int $status]: void

exit is a language construct and it can be called without parentheses if no status is passed.

Parameters

status

If status is a string, this function prints the status just before exiting.

If status is an int, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

Return Values

No value is returned.

Examples

Example #1 exit example

Example #2 exit status example

Example #3 Shutdown functions and destructors run regardless

The above example will output:

 Shutdown: shutdown[]
 Destruct: Foo::__destruct[]
 

dexen dot devries at gmail dot com

11 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

13 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:

theonenkl at gmail dot com

7 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

vincent dot laag at gmail dot com

12 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.

void a t informance d o t info

13 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

emils at tvnet dot lv

19 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.

m dot libergolis at gmail dot com

7 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.

sunfundev at gmail dot com

5 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:


// Exit into desctructor:

alexyam at live dot com

10 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:

Chủ Đề