How can I display static HTML after I have used die () in a PHP block?

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 simple. If a certain condition is met (i.e. unable to connect to the database), the program should die, but otherwise it should execute. My problem occurs when the die () function is executed. It stops right there and only sends the first three lines to the browser, but not the last two lines.

Is there a function I can use instead of die () to make the php snippets stop executing, but the static HTML text is still sent through?

+1


source to share


10 replies


You must separate your header and footer from separate files and functions. This makes the user interface easier to work with and maintains the integrity of the presentation presentation. A couple 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


source


Disconnect your programming logic from the presentation. Read about MVC, patterns.

In its simplest form, it looks like this:

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

presentation(logic());

      





In other cases, when die()

or such is unavoidable (like a fatal error or dying third-party code), there is a hack to include an output handler:

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

      

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

+7


source


Pass the static text parameter to the stamp.

For example, change this:

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

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

      

For this:

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

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

      

+2


source


I would probably use exceptions. Wrap everything in a try / catch block and then throw a new exception from the error condition, such as a database crash. You couldn't do anything in the catch block (like the empty die () method), but it would be better to present the error message here.

Here's a pretty good guide to handling exceptions in PHP5 in case you're unfamiliar with them or need to brush up on what's changed since PHP4.

+1


source


One method that works, but not exactly what I'm looking for, would be to replace die()

with die("</body></html>")

. If the return text was more complex, it could, say, be stored in a variable. Anything better than this?

0


source


die()

maybe not quite what you want here. Why not replace

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

      

from

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

      

or throw / catch an exception?

0


source


Have you looked into register_shutdown_function

(php.net) to complete the page load? It should be able to handle die()

and exit()

.

0


source


If you look at the PHP documentation , you will see that "Equivalent to exit ()" - that is, the call exits your program and doesn't really give you much opportunity to do anything. Even outside of ZF, there isn't much you can do when the application die () s. The solution is to use Exceptions.

An uncaught exception is essentially the same as die () (except for the generated stack generation). What gives you the exception is the ability to put it in a try / catch block, giving you the opportunity to fix the error and continue with the program (or show a friendly error message and create a log). Assuming you are using Zend_Controller_Front, you can check Zend Framework Quickstart to see how they make a default error handler that will catch uncaught exceptions and display an appropriate error message.

0


source


If you are working with PHP4 or just don't want to worry about exceptions, you can use this technique:

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

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

      

.. although some people seem to completely disagree with the use of this style, commenting appropriately, I don't see any problem with it. I would say this is much better than duplicating the "footer" code in each of the die () statements.

0


source


<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

      

0


source







All Articles