PHP 中的 die() 和 exit() 有什么区别?
PHP 中的 die()
和 exit()
函数有什么区别?
我认为两者具有相同的功能,但我怀疑两者有什么不同......它是什么?
What are the differences between die()
and exit()
functions in PHP?
I think both have the same functionality, but I doubt there is something different in both... what is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
没有什么区别——它们是一样的。
exit
的 PHP 手册:die
的 PHP 手册:There's no difference - they are the same.
PHP Manual for
exit
:PHP Manual for
die
:起源
差异
die()
和PHP 中的exit()
是他们的 起源。exit()
来自exit()
(C)。die()
来自die
in Perl。功能等效
die()
和exit()
是等效函数。PHP 手册
die
的 PHP 手册:exit
的 PHP 手册:函数别名列表的 PHP 手册:
与其他语言不同
die()
和exit()
在其他语言中不同,但在 PHP 中它们是相同的。来自 又一个 PHP 咆哮:
DIFFERENCE IN ORIGIN
The difference between
die()
andexit()
in PHP is their origin.exit()
is fromexit()
in C.die()
is fromdie
in Perl.FUNCTIONALLY EQUIVALENT
die()
andexit()
are equivalent functions.PHP Manual
PHP Manual for
die
:PHP Manual for
exit
:PHP Manual for List of Function Aliases:
DIFFERENT IN OTHER LANGUAGES
die()
andexit()
are different in other languages but in PHP they are identical.From Yet another PHP rant:
如前所述,这两个命令产生相同的解析器标记。
但是
有一个小的区别,那就是解析器返回令牌需要多长时间。
我没有研究过 PHP 解析器,但是如果它是一个以“d”开头的长函数列表,以及一个以“e”开头的较短列表,那么查找以“”开头的函数的函数名称肯定会花费时间。 e”。由于整个函数名称的检查方式不同,可能还会存在其他差异。
我怀疑它是可测量的,除非你有一个专门用于解析 PHP 的“完美”环境,以及大量具有不同参数的请求。
但肯定是有区别的,毕竟PHP是解释型语言。
As stated before, these two commands produce the same parser token.
BUT
There is a small difference, and that is how long it takes the parser to return the token.
I haven't studied the PHP parser, but if it's a long list of functions starting with "d", and a shorter list starting with "e", then there must be a time penalty looking up the function name for functions starting with "e". And there may be other differences due to how the whole function name is checked.
I doubt it will be measurable unless you have a "perfect" environment dedicated to parsing PHP, and a lot of requests with different parameters.
But there must be a difference, after all, PHP is an interpreted language.
die 上的 PHP 手册:
您甚至可以像
exit;
一样执行die;
- 带或不带括号。选择
die()
相对于exit()
的唯一优势可能是您可以节省输入额外字母的时间;-)PHP manual on die:
You can even do
die;
the same way asexit;
- with or without parens.The only advantage of choosing
die()
overexit()
, might be the time you spare on typing an extra letter ;-)这是非常有趣的事情。尽管
exit()
和die()
是等效的,但die()
会关闭连接。exit()
不关闭连接。die()
:exit()
:结果:
die()
:exit():
以防万一需要在您的项目中考虑到这一点。
致谢:https://stackoverflow.com/a/20932511/4357238
Here is something that's pretty interesting. Although
exit()
anddie()
are equivalent,die()
closes the connection.exit()
doesn't close the connection.die()
:exit()
:Results:
die()
:exit()
:Just incase in need to take this into account for your project.
Credits: https://stackoverflow.com/a/20932511/4357238
正如所有其他正确答案所说,
die
和exit
是相同的/别名。尽管我有一个个人惯例,当我想在预期和需要时结束脚本的执行时,我使用
exit;
。当我由于某些问题(无法连接到数据库,无法写入文件等)需要结束执行时,我使用die("Something goneError.");
来“杀死”脚本。当我使用 exit:
当我使用 die:
这样,当我在代码中的某个时刻看到
exit
时,我就知道此时我要退出,因为逻辑到此结束。当我看到
die
时,我知道我想继续执行,但由于之前执行中的错误,我不能或不应该继续执行。当然,这仅在单独处理一个项目时有效。当有更多人时,没有人会阻止他们使用
die
或exit
,这不符合我的惯例......As all the other correct answers says,
die
andexit
are identical/aliases.Although I have a personal convention that when I want to end the execution of a script when it is expected and desired, I use
exit;
. And when I need to end the execution due to some problems (couldn't connect to db, can't write to file etc.), I usedie("Something went wrong.");
to "kill" the script.When I use exit:
When I use die:
This way, when I see
exit
at some point in my code, I know that at this point I want to exit because the logic ends here.When I see
die
, I know that I'd like to continue execution, but I can't or shouldn't due to error in previous execution.Of course this only works when working on a project alone. When there is more people nobody will prevent them to use
die
orexit
where it does not fit my conventions...从功能上来说,它们是相同的,但我在以下场景中使用它们来使代码可读:
当出现错误并且必须停止执行时使用 die()。
例如
die( '哎呀!出了点问题' );
当没有错误并且必须停止执行时使用 exit()。
例如
exit('请求已成功处理!');
Functionality-wise they are identical but I use them in the following scenarios to make code readable:
Use die() when there is an error and have to stop the execution.
e.g.
die( 'Oops! Something went wrong' );
Use exit() when there is not an error and have to stop the execution.
e.g.
exit( 'Request has been processed successfully!' );
https://3v4l.org 的输出表明 die 和 exit 在功能上是相同的。
This output from https://3v4l.org demonstrates that die and exit are functionally identical.
此页面说
die
是exit
的盟友,所以它们是相同的。但也解释说:所以,你可以说我是偏执狂,但将来可能不会
死亡
。This page says
die
is an alies ofexit
, so they are identical. But also explains that:So, call me paranoid, but there may be no
die
ing in the future.它们本质上是相同的,尽管这篇文章 另有建议。
They are essentially the same, though this article suggest otherwise.
die() 的输入速度比 exit() 更快;
die() is faster to type than exit();
从功能上来说,它们是相同的。因此,选择使用哪一个完全是个人喜好。从英语语义上来说,它们是不同的。死亡听起来是否定的。当我有一个向客户端返回 JSON 数据并终止程序的函数时,如果我将此函数称为 jsonDie() 可能会很糟糕,而将其称为 jsonExit() 更合适。因此,我总是使用 exit 而不是 die。
Functionally, they are identical. So to choose which one to use is totally a personal preference. Semantically in English, they are different. Die sounds negative. When I have a function which returns JSON data to the client and terminate the program, it can be awful if I call this function jsonDie(), and it is more appropriate to call it jsonExit(). For that reason, I always use exit instead of die.
据我所知,当我查看这个问题时 这里
它说那里“在 PHP 中,标头输出存在明显差异。在下面的示例中,我选择使用不同的标头,但为了显示 exit() 和 die() 之间的差异并不重要”,并进行了测试(亲自)
From what I know when I look at this question here
It said there that "in PHP, there is a distinct difference in Header output. In the examples below I chose to use a different header but for sake of showing the difference between exit() and die() that doesn't matter", and tested (personally)
是的,它们是同卵双胞胎
,但有些开发人员按照惯例,在出现问题时使用
die
,在脚本必须停止但一切正常时使用exit
Yes, they are identical twins
But some developers by convention use
die
when something went wrong andexit
when the script must stop but everything is fine