Sign in to follow this  
ms75214

[web] php termination

Recommended Posts

how do you terminate a PHP script and ensure the footer still gets printed?

When i use a return statement, the HTML footer doesn't go out.

Share this post


Link to post
Share on other sites
[quote name='ms75214' timestamp='1298152419' post='4776441']
how do you terminate a PHP script and ensure the footer still gets printed?
[/quote]

With proper [b]exception handling[/b]!
Instead of terminating the script immediatly, throw an exception.

Imho if there is an [i]exit()[/i] or [i]die()[/i] statement somewhere in the script, it can be rewritten in a more maintainable/readable way. [i]exit()[/i] and [i]die()[/i] are like [i]GOTO[/i]. They are "evil" in terms of maintainability. You can't just rely on scope, you have to scan the whole script and every line of code for such statements.

[code]
include('header.php');

try {
...
// do sth or throw an exception
...
} catch (Exception $e) {
echo('error!');
}

include('footer.php')
[/code]

Share this post


Link to post
Share on other sites
Also: it is wise to contain all of your output in one final function so that nothing is displayed if there is an error. It also makes future maintenance much easier because your "rendering" is now separate from your logic. In anything larger than a single script, putting anything outside of <?php ?> tags becomes a no-no.

Share this post


Link to post
Share on other sites
This may not be the 'best' practice, but in my experience, this is most frequently a problem on when input is checked on a user form. You want to make sure the user hasn't inputted something illegally, and if they have, you want to show a message and kill the rest of the code.

For this, I have code similar to the following:

<?php

... some typical code included on all pages at the top (typically require_once('functions_all.php');

if(!valid_input())
{
$_SESSION['Error'] = 'The error message';
header('error.php);
exit;
}

now, error.php does something like:

make_header();
make_left();
echo $_SESSION['Error'];
make_right();
make_footer();


With this combo, I have a simple means of setting the error message for the user, and then forwarding them to a page that will display it, then killing the rest of the code with an exit statement. Based on having a set sessionID, I can call the correct menus, etc. There are many variations of this, but it ensures the page gets displayed properly, and is easy to implement throughout all my pages.

Share this post


Link to post
Share on other sites
i decided to put it all in a function called processForm. now, the return statement simply returns from the function instead of terminating everything.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this