(PHP 4, PHP 5, PHP 7, PHP 8)
exit — 使用状态 code 或消息终止当前脚本
中止脚本的执行。即使调用了 exit(),Shutdown
函数 以及 对象析构方法 始终会执行。但 finally
块永远不会执行。
退出 code 0
表示程序已成功完成任务。其它任何值都表示执行过程中发生了某种错误。
exit() 是特殊函数,因为在解析器中有个专用记号,因此可以像语句一样使用(即没有括号),以使用默认状态 code 终止脚本。
无法禁用或创建命名空间函数来屏蔽全局 exit() 函数。
status
status
是字符串,此函数将在退出前打印
status
。PHP 返回的退出 code 为 0
。
如果 status
是 int,PHP
返回的退出 code 将为 status
。
注意: 退出 code 的范围应在
0
到254
内,退出 code255
由 PHP 保留,不应使用。
由于这会终止 PHP 脚本,因此没有返回任何值。
版本 | 说明 |
---|---|
8.4.0 |
exit() 现在是真正的函数,因此遵循通用的类型处理语义,受
strict_types
声明的影响,可以用命名参数调用,并且是变量函数。
|
示例 #1 基础 exit() 示例
<?php
// 正常退出程序
exit();
exit(0);
// 带着错误 code 的退出
exit(1);
?>
示例 #2 exit() 的 string 示例
<?php
$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
or exit("unable to open file ($filename)");
?>
示例 #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()
示例 #4 exit() 作为语句
<?php
// 程序正常退出,退出 code 为 0
exit;
?>