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

查看:247
本文介绍了PHP脚本末尾是否需要die()或exit()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在php脚本中,我进行了一些测试,脚本之后是html页面.

In a php script I have some test and after the script the html page.

当测试失败时,我打电话给die(测试1失败"); 如果没有测试失败,则php脚本到达末尾?>,然后在php脚本之后加载html代码.

When a test fail i call die("Test 1 failed"); If no test fail the php script reach the end ?> and then load the html code after the php script.

这是一个好程序吗?还是我需要在PHP脚本结束之前写die()或exit()?

Is this a good procedure? Or I need to write die() or exit() before the end of php script?

推荐答案

否,您不必编写它,这不是 not 的最佳做法.如果脚本结束时没有致命错误,它将退出.

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.

如果这意味着对您进行测试",则您错了.应使用单元测试进行测试.对于php,有 phpunit .试试看,这是测试代码的正确方法.

If this means "testing" for you, you're wrong. Testing should be done using unit tests. For php there is phpunit. Give it a try, that's the proper way of testing your code.

正如CompuChip在评论中所说,退出的唯一有用用例是在编写基于php的应返回错误代码的shell脚本时. 有关exit()函数的信息,请参见文档的参数部分.

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. See the parameter section of the documentation for the exit() function.

这篇关于PHP脚本末尾是否需要die()或exit()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆