在 PHP 块中使用 die() 后如何显示静态 HTML?

发布于 2024-07-08 21:45:41 字数 412 浏览 9 评论 0原文

假设我有一些这样的代码:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

泪痕残 2024-07-15 21:45:41

将程序逻辑与表示解耦。 了解 MVC、模板。

最简单的形式是这样的:

<?php
function logic() {
    if (!$someCondition) {
        return 'display_empty_page';
    } else {
        return 'display_other_stuff';
    }
}

presentation(logic());


对于其他情况,其中 die() 或类似的情况是不可避免的(例如致命错误或第 3 方代码死亡),存在涉及输出处理程序的 hack:

ob_start('myhandler'); 
function myhandler($page) {return $page.' extra markup';}
die();

尽管我建议仅将其用于诊断/调试目的。

Decouple your program logic from presentation. Read about MVC, templates.

In simplest form it goes like that:

<?php
function logic() {
    if (!$someCondition) {
        return 'display_empty_page';
    } else {
        return 'display_other_stuff';
    }
}

presentation(logic());


For other cases, where die() or such is unavoidable (e.g. fatal error or 3rd party code dying), there's hack involving output handler:

ob_start('myhandler'); 
function myhandler($page) {return $page.' extra markup';}
die();

Although I recommend using that only for diagnostic/debugging purposes.

太阳哥哥 2024-07-15 21:45:41

您应该将页眉和页脚分成单独的文件和函数。 这使得 UI 更容易维护,并保持渲染视图的一致性。 结合使用异常处理,你就成功了。

<?php

printHeader(); // outputs the html header
try
{
    if (some condition)
    {
        throw new Exception("It Died...");
    }
    // More processing here that should not execute if the above condition is true
    // ...
}
catch (Exception e)
{
    echo $e->getMessage();
}
printFooter(); // outputs the html footer

?>

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.

<?php

printHeader(); // outputs the html header
try
{
    if (some condition)
    {
        throw new Exception("It Died...");
    }
    // More processing here that should not execute if the above condition is true
    // ...
}
catch (Exception e)
{
    echo $e->getMessage();
}
printFooter(); // outputs the html footer

?>
ヅ她的身影、若隐若现 2024-07-15 21:45:41

向模具传递静态文本的参数。

例如将其更改

<html>
<head><title>Title</title></head>
<body>

<?php
if (!$someCondition){
  die();
}
else{
  #Do something
}
?>
</body>
<html>

为:

<html>
<head><title>Title</title></head>
<body>

<?php
if (!$someCondition){
  die("OMG RED ALERT!!!!</body></html>");
}
else{
  #Do something
}
?>
</body>
<html>

Pass the die a parameter of the static text.

For example change this:

<html>
<head><title>Title</title></head>
<body>

<?php
if (!$someCondition){
  die();
}
else{
  #Do something
}
?>
</body>
<html>

To this:

<html>
<head><title>Title</title></head>
<body>

<?php
if (!$someCondition){
  die("OMG RED ALERT!!!!</body></html>");
}
else{
  #Do something
}
?>
</body>
<html>
有深☉意 2024-07-15 21:45:41

我可能会使用异常。 将所有内容包装在 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.

私藏温柔 2024-07-15 21:45:41

Have you looked into using register_shutdown_function (php.net) to finish loading the page? It should be able to handle die() and exit().

一腔孤↑勇 2024-07-15 21:45:41

如果您查看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.

写给空气的情书 2024-07-15 21:45:41

一种有效但不完全是我正在寻找的方法是将 die() 替换为 die("")< /代码>。 如果要返回的文本比这更复杂,可以将其存储在变量中。 还有比这更好的事情吗?

One method, which works but is not exactly what I'm looking for, would be to replace die() with die("</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?

执笔绘流年 2024-07-15 21:45:41

die() 可能并不完全是您想要的。 为什么不替换

if (!$someCondition) { 
    die();
} else {
    /* do stuff */
}

if ($someCondition) {
    /* do stuff */
} else {
    /* output error message/redirect/output nothing/whatever */
}

或抛出/捕获异常?

die() might not exactly be what you want here. Why not replace

if (!$someCondition) { 
    die();
} else {
    /* do stuff */
}

with

if ($someCondition) {
    /* do stuff */
} else {
    /* output error message/redirect/output nothing/whatever */
}

or throw/catch an exception?

情栀口红 2024-07-15 21:45:41

如果您正在使用 PHP4,或者只是不想为异常而烦恼,那么您可以使用这种技术:

<html>
<head><title>Title</title></head>
<body>

<?php
do {
    if (!$someCondition){
          break;
    } else {
          #Do something
    }
} while (0);
?>
</body>
<html>

.. 虽然有些人似乎非常反对使用这种风格,但经过适当的评论,我没有看到任何问题它。 我想说这比在每个 die() 语句中复制“页脚”代码要好得多。

If you're working with PHP4, or just don't want to bother with exceptions, then you could use this technique:

<html>
<head><title>Title</title></head>
<body>

<?php
do {
    if (!$someCondition){
          break;
    } else {
          #Do something
    }
} while (0);
?>
</body>
<html>

.. 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.

断桥再见 2024-07-15 21:45:41
<html>
<head><title>Title</title></head>
<body>

<?php
if (!$someCondition){
header ("location:error_page.php?erro_message='This error occured'");

  die();
}
else{
  #Do something
}
?>
</body>
<html>

错误页面.php

header
echo $_GET[$error_message];
footer
<html>
<head><title>Title</title></head>
<body>

<?php
if (!$someCondition){
header ("location:error_page.php?erro_message='This error occured'");

  die();
}
else{
  #Do something
}
?>
</body>
<html>

error_page.php

header
echo $_GET[$error_message];
footer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文