echo('exit'); 和有什么区别死;然后死(“退出”);?

发布于 2024-11-04 09:58:14 字数 407 浏览 7 评论 0原文

我见过一些代码这样做:

if(something){
    echo 'exit from program';
    die;
}
...more code

而其他代码只使用 die

if(something)   die('exit from program');
...more code

当它结束程序时是否有任何固有的区别,我应该知道它后面的代码吗?等等

更新

我主要问的是,这是否是一种编码风格,或者是否有真正的原因为什么某些编码方式与另一种编码方式不同。我不是在问 exitdie 之间有什么区别。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夕嗳→ 2024-11-11 09:58:14

不,没有什么区别;他们都会将“exit”写入 STDOUT 并终止程序。

我更喜欢 die("exit") 方法,因为它打字更少,更容易注释并且语义更清晰。

至于“速度”,你为什么要关心哪个更快呢?你需要你的程序很快就死掉吗?

回复:您的更新

...何时结束程序的任何固有差异...

没有差异,无论是固有的还是其他的。它们是相同的。第二个选项 die('exit') 是单个语句,因此与 if 语句一起使用时不需要大括号;这与模具无关,而与 C 风格语言中的块和流控制有关。

回复:您的评论/第二次更新

死亡的方式取决于个人喜好。正如我所说,它们是相同的。出于上面列出的原因,我会选择第二个选项:更短、更清晰、更干净,在我看来这相当于“更好”。

exitdie 之间的区别在于,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

... any inherent difference in when it would end the program ...

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: 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 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.

执着的年纪 2024-11-11 09:58:14

没有区别。

既然你快死了,为什么还要要求速度差异呢?

no difference.

And why asking for speed difference since you're dieing.

野却迷人 2024-11-11 09:58:14

伙计们,这是有区别的。 DIE() 可以与其他可失败的函数一起使用,而回显则需要捕获为错误或异常。

$query = mysql_query("SELECT * FROM tablename") OR DIE(mysql_error());

为您提供即时的捕捉/死亡序列。

There IS a difference guys. DIE() can be used with other failable functions whereas echoing would need to caught as an error or exception.

$query = mysql_query("SELECT * FROM tablename") OR DIE(mysql_error());

Gives you an immediate catch/die sequence.

妞丶爷亲个 2024-11-11 09:58:14

对于您发布的具体示例,它们是相等的,因为 $status 是一个字符串,但作为 手册说情况可能并不总是如此:

如果状态是字符串,则此函数
在退出之前打印状态。

如果状态是整数,则该值
将用作退出状态和
未打印。退出状态应该是
范围 0 到 254,退出状态
255 由 PHP 保留,不得
被使用。状态0用于
成功终止程序。

因此,如果您想要输出的不是 'exit from program',请说 42 您确实需要执行以下操作:

echo 42; die();

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:

If status is a string, this function
prints the status just before exiting.

If status is an integer, that value
will be used as the exit status and
not printed. Exit statuses should be
in the range 0 to 254, the exit status
255 is reserved by PHP and shall not
be used. The status 0 is used to
terminate the program successfully.

So if instead of 'exit from program' you wanted to output, say 42 you would really need to do:

echo 42; die();
守护在此方 2024-11-11 09:58:14

语言结构 exit()die() 是等效的,至少根据 PHP 手册是这样的。当应该到达该行并且我希望脚本在该点停止时,我使用 exit() 。另一方面,Die() 适用于不应该发生的情况。这对我来说是最自然的感觉,你不必同意。

The language constructs exit() and die() 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.

听风念你 2024-11-11 09:58:14

主要是编码风格。但是,如果您要输出调试消息,那么 echo then die 会更好:

echo "The frobnuticator blew up!";
die;

变成

//echo "The frobnusticator blew up!";
die;

当然,您很可能会看到

if ($debug) echo "The frobnusticator blew up!";
die;

哪个比(我|眼睛)更容易

die($debug?"The frobnusticator blew up!":"");

Mostly it's coding style. However, if you are outputting debug messages, echo then die is better:

echo "The frobnuticator blew up!";
die;

becomes

//echo "The frobnusticator blew up!";
die;

Of course, you'd most likely have

if ($debug) echo "The frobnusticator blew up!";
die;

Which is much easier on (my|the) eye than

die($debug?"The frobnusticator blew up!":"");
苍暮颜 2024-11-11 09:58:14

来自 php 手册:

注意:此语言构造等同于 die()。

dieexit 之间仍然存在区别:

使用 die() 您可以发布一个字符串:die("发生错误");

与使用 exit() 的结果相同

<?php
    echo("An error occurred <br>");
    exit(0);
?>

或者如果您是 cli 或 unix shell:

在命令行上使用 PHP,die("An error returned") 只是将“发生错误”打印到 STDOUT 并终止正常退出代码为 0 的程序。

<?php
    fwrite(STDERR, "An error occurred \n");
    exit(0); //
?>

From php manual :

Note: This language construct is equivalent to die().

But still there are difference between die and exit :

Using die() you can post a string : die("An error occurred");

Same result with using exit()

<?php
    echo("An error occurred <br>");
    exit(0);
?>

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.

<?php
    fwrite(STDERR, "An error occurred \n");
    exit(0); //
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文