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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1795025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:52:22  来源:igfitidea点击:

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

phpexitdie

提问by coderex

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

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

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

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

回答by Marek Karbarz

There's no difference - they are the same.

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

PHP Manual for exit:

PHP 手册exit

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

注意:此语言构造等效于die().

PHP Manual for die:

PHP 手册die

This language construct is equivalent to exit().

此语言构造等效于exit().

回答by Geoffrey Hale

DIFFERENCE IN ORIGIN

原产地差异

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

PHPdie()exit()PHP之间的区别在于它们的起源



FUNCTIONALLY EQUIVALENT

功能等效

die()and exit()are equivalent functions.

die()exit()等价的函数。

PHP Manual

PHP 手册

PHP Manual for die:

PHP 手册die

This language construct is equivalent to exit().

此语言构造等效于exit().

PHP Manual for exit:

PHP 手册exit

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

注意:此语言构造等效于die().

PHP Manual for List of Function Aliases:

函数别名列表的PHP 手册:

dieis an alias for master function exit()

die是主函数的别名 exit()



DIFFERENT IN OTHER LANGUAGES

其他语言的不同

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

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

From Yet another PHP rant:

来自另一个 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 Perltoo. 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.

...作为 C 和 Perl 编码员,我准备回答,“为什么,exit() 只是以数字退出状态保释程序,而 die() 将错误消息打印到 stderr 并以 EXIT_FAILURE 状态退出。 ” 但后来我想起我们正处于 PHP 混乱的语法领域。

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

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

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

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

回答by Bob

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.

我没有研究过 PHP 解析器,但是如果它是一长串以“d”开头的函数,而一个较短的以“e”开头的列表,那么查找以“电子”。由于检查整个函数名称的方式,可能还有其他差异。

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.

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

回答by Levite

PHP manual on die:

死的PHP 手册:

die — Equivalent to exit

die - 相当于退出

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

你甚至可以die;用同样的方式exit;- 有或没有括号。

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

选择die()over的唯一优势exit(),可能是您节省了输入额外字母的时间;-)

回答by Lukas

As all the other correct answers says, dieand exitare identical/aliases.

正如所有其他正确答案所说,die并且exit是相同的/别名。

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.

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

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:

当我使用 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 exitat 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.

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

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

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

回答by Edward

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

这是非常有趣的事情。虽然exit()die()是等价的,但die()关闭连接。exit()不会关闭连接。

die():

die()

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

exit():

exit()

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


Results:

结果:

exit():

exit()

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

die():

die()

HTTP/1.1 304 Not Modified 
Connection: close

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

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

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

学分:https: //stackoverflow.com/a/20932511/4357238

回答by aagjalpankaj

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.

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

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

例如die( 'Oops! Something went wrong' );

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

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

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

例如exit( 'Request has been processed successfully!' );

回答by Pedram Behroozi

This pagesays dieis an alies of exit, so they are identical. But also explains that:

此页面die是 的别名exit,因此它们是相同的。但也说明:

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.

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

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

所以,叫我偏执狂,但以后可能就没有die了。

回答by Simon77

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

https://3v4l.org 的输出表明 die 和 exit 在功能上是相同的。 在此处输入图片说明

回答by o.k.w

They are essentially the same, though this articlesuggest otherwise.

它们本质上是相同的,尽管本文提出了不同的建议。