array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.exit.php', 1 => 'exit', ), 'up' => array ( 0 => 'ref.misc.php', 1 => '杂项 函数', ), 'prev' => array ( 0 => 'function.eval.php', 1 => 'eval', ), 'next' => array ( 0 => 'function.get-browser.php', 1 => 'get_browser', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/misc/functions/exit.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
exit — 输出一个消息并且退出当前脚本
中止脚本的执行。 尽管调用了 exit(), Shutdown函数 以及 object destructors 总是会被执行。
exit
是个语法结构,如果没有 status
参数要传入,可以省略圆括号。
status
如果 status
是一个字符串,在退出之前该函数会打印
status
。
如果 status
是一个 int,该值会作为退出状态码,并且不会被打印输出。
退出状态码应该在范围0至254,不应使用被PHP保留的退出状态码255。
状态码0用于成功中止程序。
没有返回值。
示例 #1 exit() 例子
<?php
$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
or exit("unable to open file ($filename)");
?>
示例 #2 exit() 状态码例子
<?php
//exit program normally
exit;
exit();
exit(0);
//exit with an error code
exit(1);
exit(0376); //octal
?>
示例 #3 无论如何,Shutdown函数与析构函数都会被执行
<?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.';
?>
以上示例会输出:
Shutdown: shutdown() Destruct: Foo::__destruct()