导航首页 » 技术教程 » PHP:exit()的用法_misc函数
PHP:exit()的用法_misc函数 198 2023-12-15   

exit

(PHP 4, PHP 5)

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

说明

void exit ([ string $status ] ) void exit ( int $status )

中止脚本的执行。 尽管调用了 exit(), Shutdown函数 以及 object destructors 总是会被执行。

参数

status

如果 status 是一个字符串,在退出之前该函数会打印 status

如果 status 是一个 integer,该值会作为退出状态码,并且不会被打印输出。 退出状态码应该在范围0至254,不应使用被PHP保留的退出状态码255。 状态码0用于成功中止程序。

Note: PHP >= 4.2.0 当 status 是一个 integer,不会打印输出。

返回值

没有返回值。

范例

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 normally
exit;
exit();
exit(0);

//exit with an error code
exit(1);
exit(0376); //octal

?>

Example #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()
 

注释

Note: 因为是一个语言构造器而不是一个函数,不能被 可变函数 调用。

Note:

该语法结构等同于 die()。

参见

register_shutdown_function() - Register a function for execution on shutdown


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103