How Can I Display Static HTML After I've Used

2019-07-24 19:30发布

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?

标签: php die
10条回答
家丑人穷心不美
2楼-- · 2019-07-24 19:59

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?

查看更多
唯我独甜
3楼-- · 2019-07-24 20:00

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

查看更多
做个烂人
4楼-- · 2019-07-24 20:01
<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
查看更多
女痞
5楼-- · 2019-07-24 20:03

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.

查看更多
\"骚年 ilove
6楼-- · 2019-07-24 20:05

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>
查看更多
你好瞎i
7楼-- · 2019-07-24 20:07

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.

查看更多
登录 后发表回答