echo('exit'); 和有什么区别死;然后死(“退出”);?
我见过一些代码这样做:
if(something){
echo 'exit from program';
die;
}
...more code
而其他代码只使用 die
:
if(something) die('exit from program');
...more code
当它结束程序时是否有任何固有的区别,我应该知道它后面的代码吗?等等
更新
我主要问的是,这是否是一种编码风格,或者是否有真正的原因为什么某些编码方式与另一种编码方式不同。我不是在问 exit
和 die
之间有什么区别。
I have seen some code do this:
if(something){
echo 'exit from program';
die;
}
...more code
And others that just use 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
UPDATE
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,没有什么区别;他们都会将“exit”写入 STDOUT 并终止程序。
我更喜欢
die("exit")
方法,因为它打字更少,更容易注释并且语义更清晰。至于“速度”,你为什么要关心哪个更快呢?你需要你的程序很快就死掉吗?
回复:您的更新
没有差异,无论是固有的还是其他的。它们是相同的。第二个选项
die('exit')
是单个语句,因此与if
语句一起使用时不需要大括号;这与模具无关,而与 C 风格语言中的块和流控制有关。回复:您的评论/第二次更新
您
死亡
的方式取决于个人喜好。正如我所说,它们是相同的。出于上面列出的原因,我会选择第二个选项:更短、更清晰、更干净,在我看来这相当于“更好”。exit
和die
之间的区别在于,exit
允许返回非零状态,而die
返回0. 这两个功能都不是“更好”,它们服务于不同的目的。No, there is no difference; they will both write
"exit"
to STDOUT and terminate the program.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: Your update
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 anif
statement; this has nothing to do with thedie
and everything to do with blocks and flow control in C-style languages.RE: Your comment/second update
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.The difference between
exit
anddie
is thatexit
allows you to return a non-zero status, whiledie
returns 0. Neither function is "better", they serve different purposes.没有区别。
既然你快死了,为什么还要要求速度差异呢?
no difference.
And why asking for speed difference since you're dieing.
伙计们,这是有区别的。 DIE() 可以与其他可失败的函数一起使用,而回显则需要捕获为错误或异常。
为您提供即时的捕捉/死亡序列。
There IS a difference guys. DIE() can be used with other failable functions whereas echoing would need to caught as an error or exception.
Gives you an immediate catch/die sequence.
对于您发布的具体示例,它们是相等的,因为
$status
是一个字符串,但作为 手册说情况可能并不总是如此:因此,如果您想要输出的不是
'exit from program'
,请说42
您确实需要执行以下操作:For the specific example you posted they are equal, since the
$status
is a string, but as the manual says this may not always be the case:So if instead of
'exit from program'
you wanted to output, say42
you would really need to do:语言结构
exit()
和die()
是等效的,至少根据 PHP 手册是这样的。当应该到达该行并且我希望脚本在该点停止时,我使用 exit() 。另一方面,Die() 适用于不应该发生的情况。这对我来说是最自然的感觉,你不必同意。The language constructs
exit()
anddie()
are equivalent, at least according to the PHP manual. I use exit() when that line should be reached and I want the script to stop at that point. Die() on the other hand is for situations that should not occur. That's just what feels most natural for me, you don't have to agree.主要是编码风格。但是,如果您要输出调试消息,那么 echo then die 会更好:
变成
当然,您很可能会看到
哪个比(我|眼睛)更容易
Mostly it's coding style. However, if you are outputting debug messages, echo then die is better:
becomes
Of course, you'd most likely have
Which is much easier on (my|the) eye than
来自 php 手册:
但 die 和 exit 之间仍然存在区别:
与使用 exit() 的结果相同
或者如果您是 cli 或 unix shell:
在命令行上使用 PHP,die("An error returned") 只是将“发生错误”打印到 STDOUT 并终止正常退出代码为 0 的程序。
From php manual :
But still there are difference between die and exit :
Same result with using exit()
OR if you are cli or unix shell :
Using PHP on the command line, die("An error occurred") simply prints "An error occurred" to STDOUT and terminates the program with a normal exit code of 0.