PHP中的die()函数和exit()函数有什么区别?
我认为两者具有相同的功能,但我怀疑两者之间有什么不同……是什么?
PHP中的die()函数和exit()函数有什么区别?
我认为两者具有相同的功能,但我怀疑两者之间有什么不同……是什么?
当前回答
这里有一些非常有趣的东西。虽然exit()和die()是等价的,但die()关闭了连接。Exit()不会关闭连接。
die ():
<?php
header('HTTP/1.1 304 Not Modified');
die();
?>
退出():
<?php
header('HTTP/1.1 304 Not Modified');
exit();
?>
结果:
die ():
HTTP/1.1 304 Not Modified
Connection: close
退出():
HTTP/1.1 304 Not Modified
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
只是以防万一,需要把这考虑到你的项目。
学分:https://stackoverflow.com/a/20932511/4357238
其他回答
它们听起来差不多,但是exit()还允许您设置PHP脚本的退出码。
通常您并不真的需要这样做,但是在编写控制台PHP脚本时,您可能需要检查脚本是否以正确的方式完成了所有内容,例如Bash。
然后您可以使用exit()并在以后捕获它。 但是Die()不支持。
Die()在代码为0时始终存在。 因此,从本质上讲,die()命令执行以下操作:
<?php
echo "I am going to die";
exit(0);
?>
也就是:
<?php
die("I am going to die");
?>
起源差异
在PHP中die()和exit()的区别在于它们的起源。
exit()是来自C中的exit()。 die()来自Perl中的die。
的功能与
Die()和exit()是等价的函数。
PHP手册
PHP模具手册:
这个语言结构相当于exit()。
PHP退出手册:
注意:这个语言结构相当于die()。
PHP函数别名列表手册:
Die是主函数exit()的别名
不同于其他语言
die()和exit()在其他语言中是不同的,但在PHP中它们是相同的。
来自另一个PHP的咆哮:
...As a C and Perl coder, I was ready to answer, "Why, exit() just bails off the program with a numeric exit status, while die() prints out the error message to stderr and exits with EXIT_FAILURE status." But then I remembered we're in messy-syntax-land of PHP. In PHP, exit() and die() are identical. The designers obviously thought "Hmm, let's borrow exit() from C. And Perl folks probably will like it if we take die() as is from Perl too. Oops! We have two exit functions now! Let's make it so that they both can take a string or integer as an argument and make them identical!" The end result is that this didn't really make things any "easier", just more confusing. C and Perl coders will continue to use exit() to toss an integer exit value only, and die() to toss an error message and exit with a failure. Newbies and PHP-as-a-first-language people will probably wonder "umm, two exit functions, which one should I use?" The manual doesn't explain why there's exit() and die(). In general, PHP has a lot of weird redundancy like this - it tries to be friendly to people who come from different language backgrounds, but while doing so, it creates confusing redundancy.
这里有一些非常有趣的东西。虽然exit()和die()是等价的,但die()关闭了连接。Exit()不会关闭连接。
die ():
<?php
header('HTTP/1.1 304 Not Modified');
die();
?>
退出():
<?php
header('HTTP/1.1 304 Not Modified');
exit();
?>
结果:
die ():
HTTP/1.1 304 Not Modified
Connection: close
退出():
HTTP/1.1 304 Not Modified
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
只是以防万一,需要把这考虑到你的项目。
学分:https://stackoverflow.com/a/20932511/4357238
正如所有其他正确答案所说,die和exit是相同的/别名。
尽管我有一个个人约定,当我想在预期和期望的时候结束脚本的执行时,我使用exit;。当我需要结束执行由于一些问题(无法连接到db,不能写入文件等),我使用die(“Something went wrong.”);“杀死”剧本。
当我使用exit时:
header( "Location: http://www.example.com/" ); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit; // I would like to end now.
当我使用die时:
$data = file_get_contents( "file.txt" );
if( $data === false ) {
die( "Failure." ); // I don't want to end, but I can't continue. Die, script! Die!
}
do_something_important( $data );
这样,当我在代码中的某个点看到exit时,我知道在这一点上我想退出,因为逻辑在这里结束。 当我看到die时,我知道我想继续执行,但我不能或不应该由于之前执行的错误。
当然,这只适用于独自从事一个项目。当有更多的人,没有人会阻止他们使用die或exit,这不符合我的惯例…
我至少在脚本中注意到,exit()将停止当前执行的脚本,并将控制传递回任何调用脚本,而die将停止php的运行。我想说这是一个很大的不同?