在 PHP 块中使用 die() 后如何显示静态 HTML?
假设我有一些这样的代码:
<html>
<head><title>Title</title></head>
<body>
<?php
if (!$someCondition){
die();
}
else{
#Do something
}
?>
</body>
<html>
我希望这段代码的目的很简单。 如果满足某个条件(即无法连接到数据库),那么程序应该终止,否则它应该执行。 当执行 die() 函数时,我的问题出现了。 它停在那里,只将前三行发送到浏览器,但不发送最后两行。
是否有一个函数可以代替 die() 使用,以便 php 块停止执行,但静态 HTML 文本仍会发送?
Let's say I have some code like this:
<html>
<head><title>Title</title></head>
<body>
<?php
if (!$someCondition){
die();
}
else{
#Do something
}
?>
</body>
<html>
I hope the purpose of this code is straightforward. If a certain condition is met (ie can't connect to database), then the program should die, but otherwise it should execute. My problem arises when the die() function is executed. It stops right there, and sends only the first three lines to the browser, but not the last two lines.
Is there a funciton that I can use instead of die() so that the php chunks will stop executing, but the static HTML text is still sent through?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
将程序逻辑与表示解耦。 了解 MVC、模板。
最简单的形式是这样的:
对于其他情况,其中
die()
或类似的情况是不可避免的(例如致命错误或第 3 方代码死亡),存在涉及输出处理程序的 hack:尽管我建议仅将其用于诊断/调试目的。
Decouple your program logic from presentation. Read about MVC, templates.
In simplest form it goes like that:
For other cases, where
die()
or such is unavoidable (e.g. fatal error or 3rd party code dying), there's hack involving output handler:Although I recommend using that only for diagnostic/debugging purposes.
您应该将页眉和页脚分成单独的文件和函数。 这使得 UI 更容易维护,并保持渲染视图的一致性。 结合使用异常处理,你就成功了。
You should be separating out your header and footer into an separate files and functions. This makes the UI much easier to maintain and keeps things consistent for rendering the view. Couple that with using Exception handling and you're golden.
向模具传递静态文本的参数。
例如将其更改
为:
Pass the die a parameter of the static text.
For example change this:
To this:
我可能会使用异常。 将所有内容包装在 try / catch 块中,然后在出现错误情况(例如数据库故障)时抛出新的异常。 您可以在 catch 块中执行任何操作(如空 die() 方法),但最好在此处向用户显示错误消息。
这是关于 PHP5 异常处理的非常好的指南,以防您不熟悉熟悉它们,或者您需要温习自 PHP4 以来发生的变化。
I would probably use exceptions. Wrap everything in a try / catch block, then throw a new exception on an error condition like a database failure. You could do nothing in the catch block (like an empty die() method), but it would be better to present an error message to the user here.
Here's a pretty good guide on exception handling in PHP5 in case you're not familiar with them or you need to brush up on what's changed since PHP4.
您是否考虑过使用
register_shutdown_function
< /a> (php.net) 完成页面加载? 它应该能够处理die()
和exit()
。Have you looked into using
register_shutdown_function
(php.net) to finish loading the page? It should be able to handledie()
andexit()
.如果您查看PHP 文档,您会看到“相当于 exit()” - 即调用它会终止你的程序,并且不会真正给你太多做任何事情的机会。 即使在 ZF 之外,当应用程序死掉时,您也无能为力。 解决方案是使用异常。
未捕获的异常本质上与 die() 相同(除了生成的堆栈跟踪之外)。 异常为您提供了将其放入 try/catch 块的能力,使您有机会纠正错误并继续执行程序(或显示友好的错误消息并生成日志)。 假设您使用的是 Zend_Controller_Front,您可以查看 Zend Framework Quickstart 来了解它们是如何制作的默认错误处理程序将捕获未捕获的异常并显示适当的错误消息。
If you look at the PHP Documentation you'll see that "Equivalent to exit()" - ie calling it terminates your program and doesn't really give you much of a chance to do anything. Even outside of ZF, there's not a whole lot you can do when an application die()s. The solution is to use Exceptions.
An uncaught exception is essentially the same thing as a die() (other than the stack trace that gets generated). What an exception gives you is the ability to put it in a try/catch block, giving you the opportunity to correct the error and continue on with the program (or display a friendly error message and generate a log). Assuming you're using Zend_Controller_Front, you can check out the Zend Framework Quickstart to see how they make a default error handler that will catch uncaught exceptions and display an appropriate error message.
一种有效但不完全是我正在寻找的方法是将
die()
替换为die("")< /代码>。 如果要返回的文本比这更复杂,可以将其存储在变量中。 还有比这更好的事情吗?
One method, which works but is not exactly what I'm looking for, would be to replace
die()
withdie("</body></html>")
. If the text to return were more complicated than that, it could, say, be stored in a variable. Is there anything better than this?die()
可能并不完全是您想要的。 为什么不替换为
或抛出/捕获异常?
die()
might not exactly be what you want here. Why not replacewith
or throw/catch an exception?
如果您正在使用 PHP4,或者只是不想为异常而烦恼,那么您可以使用这种技术:
.. 虽然有些人似乎非常反对使用这种风格,但经过适当的评论,我没有看到任何问题它。 我想说这比在每个 die() 语句中复制“页脚”代码要好得多。
If you're working with PHP4, or just don't want to bother with exceptions, then you could use this technique:
.. though some people seem quite opposed to using this style, appropriately commented, I don't see any issues with it. I'd say it's much better than duplicating your "footer" code in each of your die() statements.
错误页面.php
error_page.php