How Can I Display Static HTML After I've Used die() in a PHP Block?

谁说胖子不能爱 提交于 2019-12-24 07:15:22

问题


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?


回答1:


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

?>



回答2:


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.




回答3:


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>



回答4:


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.




回答5:


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




回答6:


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.




回答7:


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?




回答8:


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?




回答9:


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.




回答10:


<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


来源:https://stackoverflow.com/questions/307322/how-can-i-display-static-html-after-ive-used-die-in-a-php-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!