tags:

views:

353

answers:

9

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?

A: 

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?

stalepretzel
This would quickly spiral out of control if you have anything more complex than simply </body></html>.
localshred
+1  A: 

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.

Marc Charbonneau
A: 

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?

J Cooper
+7  A: 

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.

porneL
+2  A: 

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

?>
localshred
porneL's solution is even better than the one I described, but is definitely more involved. I highly recommend MVC style architectures. To get into these types of styles you will likely need to use a framework such as Cake PHP or similar.
localshred
+1  A: 

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>
Andrew G. Johnson
This violates DRY principle.
porneL
Putting business logic in the middle of your presentation layer is also not recommended. I was answering the question asked.
Andrew G. Johnson
A: 

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

TonyUser
A: 

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.

Sean McSomething
A: 

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.

nickf
HOw is that better than the following?if($somecondition){ # do something}
Ciaran McNulty
because you can easily break at any point in your script, just like you would with die()
nickf