exit

(PHP 4, PHP 5, PHP 7, PHP 8)

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

说明

exit(string $status = ?): void
exit(int $status): void

中止脚本的执行。 尽管调用了 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()
 

注释

注意: 因为是语言构造器而不是函数,不能被 可变函数 或者 命名参数 调用。

注意:

该语法结构等同于 die()

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top