PHP中的die()函数和exit()函数有什么区别?
我认为两者具有相同的功能,但我怀疑两者之间有什么不同……是什么?
PHP中的die()函数和exit()函数有什么区别?
我认为两者具有相同的功能,但我怀疑两者之间有什么不同……是什么?
当前回答
就我所知,当我看这个问题的时候
它说:“在PHP中,头输出有明显的区别。在下面的例子中,我选择使用不同的头文件,但为了显示exit()和die()之间的差异,这无关紧要”,并测试(个人)
其他回答
如前所述,这两个命令产生相同的解析器令牌。
BUT
有一个很小的区别,那就是解析器返回令牌所需的时间。
我还没有研究过PHP解析器,但是如果它是一个以“d”开头的长列表,以及一个以“e”开头的短列表,那么查找以“e”开头的函数名必须花费一定的时间。由于整个函数名的检查方式不同,可能会有其他差异。
除非您有一个专门用于解析PHP的“完美”环境,以及大量具有不同参数的请求,否则我怀疑它是无法测量的。 但肯定有区别,毕竟PHP是一种解释性语言。
起源差异
在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将停止php的运行。我想说这是一个很大的不同?
在功能上,它们是相同的。所以选择使用哪一种完全是个人喜好。在英语的语义上,它们是不同的。死亡听起来很消极。当我有一个函数返回JSON数据到客户端并终止程序时,如果我调用这个函数jsonDie()可能会很糟糕,而调用它jsonExit()更合适。因此,我总是使用exit而不是die。
PHP模具手册:
die -相当于退出
你甚至可以死去;与退出的方式相同;-有或没有paren。
选择die()而不是exit()的唯一好处可能是你可以节省输入一个额外的字母的时间;-)