php exit, exit(), exit(0), die(), die(0) - 如何退出脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8031921/
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-26 03:49:29  来源:igfitidea点击:

exit, exit(), exit(0), die(), die(0) - How to exit script

php

提问by Explosion Pills

I believe that all of these (and even die()or die(0)) are identical. If they are not identical, which is preferred for exiting a script successfully? If they are identical, is there any preferred standard to indicate successful script completion? I tend to use exit;.

我相信所有这些(甚至die()die(0))都是相同的。如果它们不相同,哪个是成功退出脚本的首选?如果它们相同,是否有任何首选标准来指示脚本成功完成?我倾向于使用exit;.

EDIT: All of the answers have "die()and exit()are identical" even though I say that in my question. I updated to the title to hopefully make it clearer that this is NOT my question. I want to clearly indicate success from a command line script.

编辑:即使我在我的问题中这么说,所有的答案都有“die()并且exit()是相同的”。我更新了标题,希望能更清楚地表明这不是我的问题。我想从命令行脚本中清楚地表明成功。

采纳答案by AgentConundrum

These are all identical. I'm pretty sure die()is just a straight-up alias to exit(), but even if it isn't, it still acts identically.

这些都是相同的。我很确定die()这只是 的直接别名exit(),但即使不是,它的作用仍然相同。

When one of these functions is given a string argument, it prints out the string before terminating the process. When it encounters an integer under 255, that integer is considered the return code for the process, which gets passed back to the process which invoked the PHP script. This is particularly useful when writing command line applications (PHP isn't web-only!).

当这些函数之一被赋予一个字符串参数时,它会在终止进程之前打印出该字符串。当它遇到一个小于 255 的整数时,该整数被认为是进程的返回码,它被传递回调用 PHP 脚本的进程。这在编写命令行应用程序时特别有用(PHP 不是仅限 Web 的!)。

As far as the difference between exit, exit(), and exit(0), there really is none. There is definitely no difference between the first two because exitis technically a language construct, not a function, so it can be called with or without parentheses, just like echo. Returning a code of 0means "this program ran successfully/without errors", and while I don't know what exactly happens when you don't pass an argument, PHP.netsays that an argument-less exitindicates success, so I would bet it returns 0, though again PHP.netdoesn't show a default for the argument.

至于exit, exit(), 和之间的区别exit(0),确实没有。前两者绝对没有区别,因为exit从技术上讲是一种语言结构,而不是函数,因此可以带括号或不带括号调用它,就像echo. 返回一个0表示“这个程序运行成功/没有错误”的代码,虽然我不知道当你不传递参数时到底会发生什么,PHP.net说没有参数exit表示成功,所以我敢打赌它返回0,尽管PHP.net再次没有显示参数的默认值。

回答by nsanders

As several people have mentioned, die() and exit() are exactly the same.

正如一些人提到的, die() 和 exit() 完全相同。

If you look at the PHP documentation, there are two options for arguments:

如果您查看PHP 文档,则有两个参数选项:

  • An numeric value. This is only useful if you are using PHP from the command line, as opposed to a web server. A value of zero indicates success. Nonzero indicates a failure condition occurred.

  • A string value. This will be displayed to the browser when the exit occurs.

  • 一个数值。这仅在您从命令行使用 PHP 而不是 Web 服务器时才有用。零值表示成功。非零表示发生了故障情况。

  • 一个字符串值。这将在退出发生时显示给浏览器。

Instead of die() or exit(), I recommend using exceptions and a custom top-level exception handlerto manage failure conditions.

我建议使用异常和自定义顶级异常处理程序来管理失败条件,而不是 die() 或 exit() 。

You have more flexibility that way to do things like automatic error logging. Also, if you're using PHP to implement a JSON API, this exception handler can hand back a valid, error-indicating JSON snippet instead.

您可以更灵活地执行自动错误记录等操作。此外,如果您使用 PHP 来实现 JSON API,则此异常处理程序可以转回有效的、指示错误的 JSON 片段。

回答by jjyepez

I would say that in regards with a better semantics die($arg);should be used for an abnormal or unexpected termination, even when -of course- you still have caught it. And exit($arg);should be used for a normal (expected / controlled) end of a process, like in break;for a foror whileor a switchstructure but with a definitive end.

我会说,对于die($arg);异常或意外终止,应该使用更好的语义,即使 - 当然 - 你仍然抓住了它。并且exit($arg);应该用于流程的正常(预期/受控)结束,例如break;用于forwhileswitch结构但具有确定的结束。

Nevertheless .. I personally often use a general if { } else { }structure to control different branches of huge processes or output buffering so not having to use "exit" ..

尽管如此..我个人经常使用通用if { } else { }结构来控制大型进程或输出缓冲的不同分支,因此不必使用“退出”..

I also use die($arg)in simple error-catching semantics like in

我还使用die($arg)简单的错误捕获语义,如

$db = mysql_connect([$args]) or die ($error);...

$db = mysql_connect([$args]) or die ($error);...

回答by NVRM

I will get downvoted to hell, but in some cases when hacking in CLI, we do not want the program to get killed, while not wanting to continue the full execution.

我会被否决到地狱,但在某些情况下,当在 CLI 中进行黑客攻击时,我们不希望程序被杀死,同时也不想继续完整执行。

Here the goal is to avoid making api calls to a separate hand-point file. Say I have a nice play button in my interface, that execute system calls.

这里的目标是避免对单独的hand-point 文件进行api 调用。假设我的界面中有一个很好的播放按钮,用于执行系统调用。

Example 1: The program get killedafter the job , no datas returned. This is not wanted.

示例 1:程序在作业后被杀死,没有数据返回。这不是想要的。

if ($_GET["play"] != ""){
  // Some job
  exit; 
}
/* Huge amount of data*/ 

Example 2: The program still runs, feeding the whole data in the GET request. This is unnecessary on this case. This is slowing down the browser with all the data, that he has already.

示例 2:程序仍在运行,在 GET 请求中提供整个数据。在这种情况下这是不必要的。这使浏览器的所有数据变慢,他已经这样做了。

if ($_GET["play"] != ""){
  // Some job
}
/* Huge amount of data*/ 

Example 3: The program still runs, no data returned as expected, the play command had been executed, but the whole data set get parsed, this is unnecessary job, can slow down php/the machine.

例3:程序还在运行,没有按预期返回数据,播放命令已经执行,但是整个数据集都被解析了,这是不必要的工作,会减慢php/机器的速度。

/* Huge amount of data*/ 
if ($_GET["play"] != ""){
  // Some job
}

Example 4: The program still runs, no data returned as expected, the play command had been executed, the whole data had not been parsed, php returned super quickly 200OK with an empty response, as expected. Everyone happy!

例子4:程序还在运行,没有按预期返回数据,播放命令已经执行,整个数据没有被解析,php快速返回200OK,响应为空,符合预期。大家开心!

if ($_GET["play"] != ""){
  // Some job
  goto END;
}
/* Huge amount of data*/
END;

Yes! Using GOTO, sometimes is to be considered -as the best to do -!

是的!使用 GOTO,有时被认为是 - 最好的做法 -!

https://www.php.net/manual/en/control-structures.goto.php

https://www.php.net/manual/en/control-structures.goto.php

https://i.stack.imgur.com/kQnqC.png

https://i.stack.imgur.com/kQnqC.png

回答by slugonamission

die();is just a synonym for exit();and is functionally identical.

die();只是同义词exit();并且在功能上是相同的。

The standard way is to use exit code zero to signify success, and anything else to denote an error condition.

标准方法是使用退出代码零来表示成功,并使用其他任何东西来表示错误条件。

回答by xthexder

die() is typically used to kill the script with an error output:

die() 通常用于杀死带有错误输出的脚本:

die("There was a fatal error");

where-as exit is typically used as a successful exit (At least in my coding)

where-as exit 通常用作成功退出(至少在我的编码中)

The PHP Manualsays that the functions are identical.

PHP手册说,功能是相同的。

回答by Hamish

dieis exactly equivalent to exit.

die完全等同于exit.

From the manual:

从手册

If status is an integer, that value will be used as the exit status..

如果 status 是整数,则该值将用作退出状态。

This is only useful if you have some sort of wrapper that does something based on the exit status. Unless you have a specific need to report an exit code to the outside world, just exit;.

这仅在您有某种基于退出状态执行某些操作的包装器时才有用。除非您有特定的需要向外界报告退出代码,否则只需exit;.