简体   繁体   English

PHP脚本末尾是否需要die()或exit()函数?

[英]Is the die() or exit() function needed in the end of a php script?

In a php script I have some test and after the script the html page. 在php脚本中,我进行了一些测试,并在脚本之后找到了html页面。

When a test fail i call die("Test 1 failed"); 当测试失败时,我会调用die(“测试1失败”); If no test fail the php script reach the end ?> and then load the html code after the php script. 如果没有测试失败,则php脚本到达末尾?>,然后在php脚本之后加载html代码。

Is this a good procedure? 这是个好程序吗? Or I need to write die() or exit() before the end of php script? 还是我需要在PHP脚本结束之前写die()或exit()?

No you don't have to write that and this is not best practice. 不,您不必写那也不是最佳实践。 If the script reaches the end without fatal errros it will exit. 如果脚本到达最后而没有致命错误,它将退出。

If this means "testing" for you, you're wrong. 如果这对您来说意味着“测试”,那您就错了。 Testing should be done using unit tests . 测试应该使用单元测试来完成。 For php there is phpunit . 对于php有phpunit Give it a try, that's the proper way of testing your code. 试试看,这是测试代码的正确方法。

Edit: As CompuChip says in a comment, the only useful use case for exit is when you're writing a php based shell script that should return an error code. 编辑:正如CompuChip在评论中所说,退出的唯一有用用例是当您编写基于php的应返回错误代码的shell脚本时。 See the parameter section of the documentation for the exit() function . 有关exit()函数,请参见文档的参数部分

No you don't need that, but when writing console PHP scripts, you might want to check with for example Bash if the script completed everything in the right way. 不,您不需要这样做,但是在编写控制台PHP脚本时,您可能需要检查例如Bash是否脚本以正确的方式完成了所有操作。 That's when you use exit() or die() 那是当您使用exit()或die()时

Is the die() or exit() function needed in the end of a php script? PHP脚本末尾是否需要die()或exit()函数?

No, PHP will end the script itself. 不,PHP将结束脚本本身。 If the script is an included file (called from another file) then it will end script in the included file and then continue with any code in the original file after where you included (if there is any code). 如果脚本是包含的文件(从另一个文件调用),则它将在包含的文件中结束脚本,然后在包含位置之后继续原始文件中的任何代码(如果有任何代码)。

So you put die() or exit() where ever you want or need it. 因此,您可以将die()exit()放在所需或需要的位置。

For testing, put it after each block of code you test. 为了进行测试,请将其放在要测试的每个代码块之后。 I use them in some parts of testing if I just want PHP to show me something then stop, such as print out an array to make sure it's being constructed correctly etc. 如果我只是想让PHP向我展示一些内容然后停止,那么我将在测试的某些部分中使用它们,例如打印出数组以确保其正确构建等。
eg: 例如:

print_r($array);
exit();

For other code tests, I sometimes just echo "Section A worked", etc, such as within if/else. 对于其他代码测试,有时我会仅在“ if / else”内回显“ A节已工作”等。 If I want to know if a particular part of code is working or if some criteria is being met or not (basically, it lets you trace where PHP itself is going within your code). 如果我想知道代码的特定部分是否正常工作,或者是否满足某些条件(基本上,它使您可以跟踪PHP本身在代码中的运行位置)。

All that said, don't use die() or exit() in production code. 所有这些,不要在生产代码中使用die()exit() You should use a more friendly and controlled messaging setup. 您应该使用更友好和受控的消息传递设置。 For security reasons and visual, as you could potentially give them some info like "ERROR Failed to load SomethingSecret". 出于安全原因和视觉上的考虑,您可能会给他们一些信息,例如“错误无法加载SomethingSecret”。 Also it doesn't look pretty when you page only half loads and then puts out an on screen error message which likely means nothing to the end user. 同样,当您仅分页加载一半,然后发出屏幕错误消息时,它看起来并不漂亮,这对最终用户可能毫无意义。

Have a read through this: 阅读以下内容:
PHP Error handling: die() Vs trigger_error() Vs throw Exception PHP错误处理:die()VS trigger_error()VS throw异常

不,您不必在脚本的末尾使用这些功能,因为它始终存在于脚本的末尾。

You should never be using die() or exit in your production PHP scripts except in very specific cases. 除非在非常特殊的情况下,否则永远不要使用die()或在生产PHP脚本中exit Instead, re-work your code paths to simply show an error message to the user rather than exiting the script early. 而是,重新设计您的代码路径以仅向用户显示错误消息,而不是提早退出脚本。

No ! 不行 This is not recommanded to use it 不建议使用它

Use trigger_error or error_log to log the tests in your error.log. 使用trigger_errorerror_log将测试记录在error.log中。 Then check it. 然后检查一下。

No need to put a die or an exit at the end of the scipt. 无需在模版结尾处放置骰子或退出。 But you may use exit to terminate your script with a specific exit code (by default it's 0). 但是您可以使用exit终止具有特定退出代码的脚本(默认情况下为0)。

Eg 例如

$ php -r "/* does nothing */;"
$ echo $?
0
$ php -r "exit(123);"
$ echo $?
123

http://php.net/exit http://php.net/exit

From the documentation: 从文档中:

The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close() . 脚本执行结束后,将立即关闭到服务器的链接,除非更早地通过显式调用mysql_close()关闭了该链接。

是的,如果要运行其他代码(例如HTML代码),则无需调用die()或exit(0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM