百科狗-知识改变命运!

exit() - php 选项信息函数

百变鹏仔2年前 (2023-11-21)阅读数 26#技术干货
文章标签函数

exit()

(PHP 4, PHP 5, PHP 7)

输出一个消息并且退出当前脚本

说明

exit([string $status]): voidexit(int $status): void

中止脚本的执行。尽管调用了exit(),Shutdown函数以及object destructors总是会被执行。

exit是个语法结构,如果没有$status参数要传入,可以省略圆括号。

参数

$status

如果$status是一个字符串,在退出之前该函数会打印$status。

如果$status是一个integer,该值会作为退出状态码,并且不会被打印输出。退出状态码应该在范围0至254,不应使用被PHP保留的退出状态码255。状态码0用于成功中止程序。

返回值

没有返回值。

范例

Example #1exit()例子


Example #2exit()状态码例子


无论如何,Shutdown函数与析构函数都会被执行

exit() - php 选项信息函数


以上例程会输出:

 Shutdown: shutdown()
 Destruct: Foo::__destruct()
 

注释

Note:因为是一个语言构造器而不是一个函数,不能被可变函数调用。

Note:

该语法结构等同于die()。

参见

  • register_shutdown_function()注册一个会在php中止时执行的函数
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.

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:

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

Calling to exit() will flush all buffers started by ob_start() to default output.
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');
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.
return may be preferable to exit in certain situations, especially when dealing with the PHP binary and the shell.
I have a script which is the recipient of a mail alias, i.e. mail sent to that alias is piped to the script instead of being delivered to a mailbox. Using exit in this script resulted in the sender of the email getting a delivery failure notice. This was not the desired behavior, I wanted to silently discard messages which did not satisfy the script's requirements.
After several hours of trying to figure out what integer value I should pass to exit() to satisfy sendmail, I tried using return instead of exit. Worked like a charm. Sendmail didn't like exit but it was perfectly happy with return. So, if you're running into trouble with exit and other system binaries, try using return instead.
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:

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)