exit() - php 选项信息函数
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函数与析构函数都会被执行
以上例程会输出:
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:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)