Php/docs/function.exit

来自菜鸟教程
跳转至:导航、​搜索

exit

(PHP 4, PHP 5, PHP 7)

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用于成功中止程序。


返回值

没有返回值。


范例

Example #1 exit() 例子

<?php$filename = '/path/to/data-file';$file = fopen($filename, 'r')    or exit("unable to open file ($filename)");?>

Example #2 exit() 状态码例子

<?php//exit program normallyexit;exit();exit(0);//exit with an error codeexit(1);exit(0376); //octal?>

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

<?phpclass 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()
 

注释

Note:

因为是一个语言构造器而不是一个函数,不能被

可变函数 调用。

Note:

该语法结构等同于 die()

参见