先说结论:PHP中的die和exit是没有区别的,其实调用的是同一个函数:exit()。
函数如下:void exit ([ string
status])voidexit(int
status )
中止脚本的执行。 尽管调用了 exit(), shutdown函数 以及 类中的析构函数 总是会被执行。
exit 是个语法结构,如果没有 status 参数要传入,可以省略圆括号。
对于 $status,如果为string,在脚本退出时会将其打印出来。如果为int,则不打印。
对于int类型的几点说明:
其中1、2 引自http://php.net/manual/zh/function.exit.php
- 如果 status 是一个 integer,该值会作为退出状态码,并且不会被打印输出。
- 退出状态码应该在范围0至254,不应使用被PHP保留的退出状态码255。 状态码0用于成功中止程序。
- 状态码可以在注册shutdown函数后使用
- 在命令行执行
php -r 'exit(10);' //空 echo $? //10
说明exit中的状态码被放入了当前执行它的环境中去,蛋似不晓得如何使用 - -
引用一个手册上的栗子:
http://php.net/manual/zh/function.exit.php
无论如何,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';
?>