PHP中的die()函数和exit()函数有什么区别?
我认为两者具有相同的功能,但我怀疑两者之间有什么不同……是什么?
PHP中的die()函数和exit()函数有什么区别?
我认为两者具有相同的功能,但我怀疑两者之间有什么不同……是什么?
当前回答
exit()函数和die()函数的结果总是相同的。但正如别名手册页(http://php.net/manual/en/aliases.php)所解释的那样,它说die()函数调用exit函数。我认为它是硬编码如下:
function die($msg){
exit($msg);
}
这不是小型、中型和大型项目的性能问题,但如果项目有几十亿几十亿的过程,这是非常重要的性能优化状态。
但大多数人不认为这是一个问题,因为如果你有那么多进程,你肯定比一个函数是master或alias考虑更多的问题。
确切的答案是;总是主函数比别名更快。
最后;别名手册页上说,你可能不再使用死亡。它只是一个别名,不推荐使用。
使用这类别名通常是一个坏主意,因为它们可能是 注定要过时或重命名,这将导致不可移植 脚本。此列表是为了帮助那些想要升级他们的 旧脚本到新语法。
其他回答
起源差异
在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.
这个页面说die是一个退出,所以它们是相同的。但也解释了:
有些函数因为API清理或其他原因更改了名称,旧的名称仅作为向后兼容的别名保留。使用这类别名通常不是一个好主意,因为它们可能会过时或重命名,这将导致不可移植的脚本。
所以,你可以说我是偏执狂,但未来可能不会有死亡。
exit()函数和die()函数的结果总是相同的。但正如别名手册页(http://php.net/manual/en/aliases.php)所解释的那样,它说die()函数调用exit函数。我认为它是硬编码如下:
function die($msg){
exit($msg);
}
这不是小型、中型和大型项目的性能问题,但如果项目有几十亿几十亿的过程,这是非常重要的性能优化状态。
但大多数人不认为这是一个问题,因为如果你有那么多进程,你肯定比一个函数是master或alias考虑更多的问题。
确切的答案是;总是主函数比别名更快。
最后;别名手册页上说,你可能不再使用死亡。它只是一个别名,不推荐使用。
使用这类别名通常是一个坏主意,因为它们可能是 注定要过时或重命名,这将导致不可移植 脚本。此列表是为了帮助那些想要升级他们的 旧脚本到新语法。
在功能上,它们是相同的。所以选择使用哪一种完全是个人喜好。在英语的语义上,它们是不同的。死亡听起来很消极。当我有一个函数返回JSON数据到客户端并终止程序时,如果我调用这个函数jsonDie()可能会很糟糕,而调用它jsonExit()更合适。因此,我总是使用exit而不是die。
在功能方面,它们是相同的,但我在以下场景中使用它们以使代码可读:
当出现错误并必须停止执行时使用die()。
如。 死亡(“哦!出问题了’);
当没有错误并且必须停止执行时,使用exit()。
如。 exit('请求已成功处理!”);