PHP 中的 die() 和 exit() 有什么区别?

发布于 2024-08-12 12:12:30 字数 107 浏览 4 评论 0原文

PHP 中的 die()exit() 函数有什么区别?

我认为两者具有相同的功能,但我怀疑两者有什么不同......它是什么?

What are the differences between die() and exit() functions in PHP?

I think both have the same functionality, but I doubt there is something different in both... what is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(14

不语却知心 2024-08-19 12:12:30

没有什么区别——它们是一样的。

exit 的 PHP 手册:

注意:此语言构造相当于 die().


die 的 PHP 手册:

此语言构造相当于 exit()

There's no difference - they are the same.

PHP Manual for exit:

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

PHP Manual for die:

This language construct is equivalent to exit().

孤君无依 2024-08-19 12:12:30

起源

差异 die() 和PHP 中的 exit() 是他们的 起源。


功能等效

die()exit()等效函数。

PHP 手册

die 的 PHP 手册:

此语言构造相当于exit().


exit 的 PHP 手册:

注意:此语言构造相当于 die() .

函数别名列表的 PHP 手册:

die 是 master 函数的别名exit()


与其他语言不同

die()exit() 在其他语言中不同,但在 PHP 中它们是相同的。

来自 又一个 PHP 咆哮

...作为一名 C 和 Perl 编码员,我准备回答:“为什么,exit() 只是保释
以数字退出状态关闭程序,而 die() 打印出
向 stderr 发送错误消息并以 EXIT_FAILURE 状态退出。”但是随后
我记得我们正处于 PHP 语法混乱的境地。

在 PHP 中,exit() 和 die() 是相同的。

设计者显然认为“嗯,让我们借用C的exit()和珀尔
如果我们也采用 Perl 中的 die() ,人们可能会喜欢它。
哎呀!我们现在有两个退出函数!让我们让他们俩都
可以采用字符串或整数作为参数并使它们相同!”

最终的结果是,这并没有真正让事情变得“更容易”,
只是更令人困惑。 C 和 Perl 程序员将继续使用 exit() 来
仅抛出一个整数退出值,并用 die() 抛出错误消息
并失败退出。新手和 PHP 作为第一语言的人
可能会想知道“嗯,两个退出函数,我应该选择哪一个
使用?”手册没有解释为什么有 exit() 和 die()。

一般来说,PHP 有很多像这样的奇怪的冗余 - 它试图
对来自不同语言背景的人友好,
但这样做时,它会产生令人困惑的冗余。

DIFFERENCE IN ORIGIN

The difference between die() and exit() in PHP is their origin.


FUNCTIONALLY EQUIVALENT

die() and exit() are equivalent functions.

PHP Manual

PHP Manual for die:

This language construct is equivalent to exit().

PHP Manual for exit:

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

PHP Manual for List of Function Aliases:

die is an alias for master function exit()


DIFFERENT IN OTHER LANGUAGES

die() and exit() are different in other languages but in PHP they are identical.

From Yet another PHP rant:

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

路弥 2024-08-19 12:12:30

如前所述,这两个命令产生相同的解析器标记。

但是

有一个小的区别,那就是解析器返回令牌需要多长时间。

我没有研究过 PHP 解析器,但是如果它是一个以“d”开头的长函数列表,以及一个以“e”开头的较短列表,那么查找以“”开头的函数的函数名称肯定会花费时间。 e”。由于整个函数名称的检查方式不同,可能还会存在其他差异。

我怀疑它是可测量的,除非你有一个专门用于解析 PHP 的“完美”环境,以及大量具有不同参数的请求。
但肯定是有区别的,毕竟PHP是解释型语言。

As stated before, these two commands produce the same parser token.

BUT

There is a small difference, and that is how long it takes the parser to return the token.

I haven't studied the PHP parser, but if it's a long list of functions starting with "d", and a shorter list starting with "e", then there must be a time penalty looking up the function name for functions starting with "e". And there may be other differences due to how the whole function name is checked.

I doubt it will be measurable unless you have a "perfect" environment dedicated to parsing PHP, and a lot of requests with different parameters.
But there must be a difference, after all, PHP is an interpreted language.

深海少女心 2024-08-19 12:12:30

die 上的 PHP 手册:

die——相当于退出

您甚至可以像 exit; 一样执行 die; - 带或不带括号。

选择 die() 相对于 exit() 的唯一优势可能是您可以节省输入额外字母的时间;-)

PHP manual on die:

die — Equivalent to exit

You can even do die; the same way as exit; - with or without parens.

The only advantage of choosing die() over exit(), might be the time you spare on typing an extra letter ;-)

软糯酥胸 2024-08-19 12:12:30

这是非常有趣的事情。尽管 exit()die() 是等效的,但 die()关闭连接。 exit() 不关闭连接。

die():

<?php
    header('HTTP/1.1 304 Not Modified');
    die();
?>

exit():

<?php
    header('HTTP/1.1 304 Not Modified');
    exit();
?>

结果:

die():

HTTP/1.1 304 Not Modified 
Connection: close

exit():

HTTP/1.1 304 Not Modified 
Connection: Keep-Alive 
Keep-Alive: timeout=5, max=100

以防万一需要在您的项目中考虑到这一点。

致谢:https://stackoverflow.com/a/20932511/4357238

Here is something that's pretty interesting. Although exit() and die() are equivalent, die() closes the connection. exit() doesn't close the connection.

die():

<?php
    header('HTTP/1.1 304 Not Modified');
    die();
?>

exit():

<?php
    header('HTTP/1.1 304 Not Modified');
    exit();
?>

Results:

die():

HTTP/1.1 304 Not Modified 
Connection: close

exit():

HTTP/1.1 304 Not Modified 
Connection: Keep-Alive 
Keep-Alive: timeout=5, max=100

Just incase in need to take this into account for your project.

Credits: https://stackoverflow.com/a/20932511/4357238

终止放荡 2024-08-19 12:12:30

正如所有其他正确答案所说,dieexit 是相同的/别名。

尽管我有一个个人惯例,当我想在预期和需要时结束脚本的执行时,我使用 exit;。当我由于某些问题(无法连接到数据库,无法写入文件等)需要结束执行时,我使用 die("Something goneError."); 来“杀死”脚本。

当我使用 exit:

header( "Location: http://www.example.com/" ); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit; // I would like to end now.

当我使用 die:

$data = file_get_contents( "file.txt" );
if( $data === false ) {
    die( "Failure." ); // I don't want to end, but I can't continue. Die, script! Die!
}
do_something_important( $data );

这样,当我在代码中的某个时刻看到 exit 时,我就知道此时我要退出,因为逻辑到此结束。
当我看到 die 时,我知道我想继续执行,但由于之前执行中的错误,我不能或不应该继续执行。

当然,这仅在单独处理一个项目时有效。当有更多人时,没有人会阻止他们使用 dieexit ,这不符合我的惯例......

As all the other correct answers says, die and exit are identical/aliases.

Although I have a personal convention that when I want to end the execution of a script when it is expected and desired, I use exit;. And when I need to end the execution due to some problems (couldn't connect to db, can't write to file etc.), I use die("Something went wrong."); to "kill" the script.

When I use exit:

header( "Location: http://www.example.com/" ); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit; // I would like to end now.

When I use die:

$data = file_get_contents( "file.txt" );
if( $data === false ) {
    die( "Failure." ); // I don't want to end, but I can't continue. Die, script! Die!
}
do_something_important( $data );

This way, when I see exit at some point in my code, I know that at this point I want to exit because the logic ends here.
When I see die, I know that I'd like to continue execution, but I can't or shouldn't due to error in previous execution.

Of course this only works when working on a project alone. When there is more people nobody will prevent them to use die or exit where it does not fit my conventions...

单调的奢华 2024-08-19 12:12:30

从功能上来说,它们是相同的,但我在以下场景中使用它们来使代码可读:

当出现错误并且必须停止执行时使用 die()。

例如
die( '哎呀!出了点问题' );

当没有错误并且必须停止执行时使用 exit()。

例如
exit('请求已成功处理!');

Functionality-wise they are identical but I use them in the following scenarios to make code readable:

Use die() when there is an error and have to stop the execution.

e.g.
die( 'Oops! Something went wrong' );

Use exit() when there is not an error and have to stop the execution.

e.g.
exit( 'Request has been processed successfully!' );

耶耶耶 2024-08-19 12:12:30

https://3v4l.org 的输出表明 die 和 exit 在功能上是相同的。
输入图像描述这里

This output from https://3v4l.org demonstrates that die and exit are functionally identical.
enter image description here

压抑⊿情绪 2024-08-19 12:12:30

此页面dieexit的盟友,所以它们是相同的。但也解释说:

有些函数由于 API 清理或其他原因而更改了名称,并且旧名称仅保留为别名以实现向后兼容性。使用此类别名通常是一个坏主意,因为它们可能会过时或重命名,从而导致脚本不可移植。

所以,你可以说我是偏执狂,但将来可能不会死亡

This page says die is an alies of exit, so they are identical. But also explains that:

there are functions which changed names because of an API cleanup or some other reason and the old names are only kept as aliases for backward compatibility. It is usually a bad idea to use these kind of aliases, as they may be bound to obsolescence or renaming, which will lead to unportable script.

So, call me paranoid, but there may be no dieing in the future.

悍妇囚夫 2024-08-19 12:12:30

它们本质上是相同的,尽管这篇文章 另有建议。

They are essentially the same, though this article suggest otherwise.

浅笑依然 2024-08-19 12:12:30

die() 的输入速度比 exit() 更快;

die() is faster to type than exit();

浪漫人生路 2024-08-19 12:12:30

从功能上来说,它们是相同的。因此,选择使用哪一个完全是个人喜好。从英语语义上来说,它们是不同的。死亡听起来是否定的。当我有一个向客户端返回 JSON 数据并终止程序的函数时,如果我将此函数称为 jsonDie() 可能会很糟糕,而将其称为 jsonExit() 更合适。因此,我总是使用 exit 而不是 die。

Functionally, they are identical. So to choose which one to use is totally a personal preference. Semantically in English, they are different. Die sounds negative. When I have a function which returns JSON data to the client and terminate the program, it can be awful if I call this function jsonDie(), and it is more appropriate to call it jsonExit(). For that reason, I always use exit instead of die.

肥爪爪 2024-08-19 12:12:30

据我所知,当我查看这个问题时 这里

它说那里“在 PHP 中,标头输出存在明显差异。在下面的示例中,我选择使用不同的标头,但为了显示 exit() 和 die() 之间的差异并不重要”,并进行了测试(亲自)

From what I know when I look at this question here

It said there that "in PHP, there is a distinct difference in Header output. In the examples below I chose to use a different header but for sake of showing the difference between exit() and die() that doesn't matter", and tested (personally)

美男兮 2024-08-19 12:12:30

是的,它们是同卵双胞胎

,但有些开发人员按照惯例,在出现问题时使用 die,在脚本必须停止但一切正常时使用 exit

Yes, they are identical twins

But some developers by convention use die when something went wrong and exit when the script must stop but everything is fine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文