问题描述
我已经看到一些代码可以这样做:
I have seen some code do this:
if(something){
echo 'exit from program';
die;
}
...more code
其他仅使用 die
:
if(something) die('exit from program');
...more code
结束程序的时间是否有内在差异,我应该知道它后面的代码吗?等等
Is there any inherent difference in when it would end the program, should I be aware of the code that comes after it? etcetera
更新
我主要是问这是否是一种编码样式,或者是否有真正的原因为什么某些编码方式是另一种编码方式。我不是问 exit
和 die
之间有什么区别。
I am asking mainly, if it is a coding style, or if there is a real reason why some is coded one way versus another. I am not asking what the difference between exit
and die
is.
推荐答案
不,没有区别;他们都将 exit
写入STDOUT并终止程序。
No, there is no difference; they will both write "exit"
to STDOUT and terminate the program.
我更喜欢 die( exit)
方法,因为它的键入较少,更易于注释和语义
I would prefer the die("exit")
method as it's less typing, easier to comment out and semantically clearer.
就速度而言,为什么还要关心哪个更快?您是否需要程序迅速消失?
As far as "speed", why would you care which is faster? Do you need your program to die really quickly?
RE:您的更新
有 die('exit')是单个语句,因此与 if $ c $一起使用时不需要大括号。 c>声明;这与
die
无关,与C风格语言中的块和流控制无关。
There is no difference, inherent or otherwise. They're identical. The second option, die('exit')
, is a single statement, and so requires no braces when used with an if
statement; this has nothing to do with the die
and everything to do with blocks and flow control in C-style languages.
RE:您的评论/第二次更新
您死亡
的哪种方式很重要个人喜好。正如我所说的,它们是相同的。我会出于上述原因选择第二个选项:更短,更清晰,更干净,在我看来这相当于更好。
Which way you die
is a matter of personal preference. As I said, they are identical. I would choose the 2nd option for the reasons listed above: Shorter, clearer, cleaner, which amounts to "better" in my opinion.
<$ c $之间的差额c> exit 和 die
是 exit
允许您返回非零状态,而 die
返回0。这两个函数都不是更好的,它们有不同的用途。
The difference between exit
and die
is that exit
allows you to return a non-zero status, while die
returns 0. Neither function is "better", they serve different purposes.
这篇关于echo('exit');和有什么区别?死;死('退出');?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!