Jump to content
  • Advertisement
Sign in to follow this  
ms75214

[web] php termination

This topic is 2773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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


With proper exception handling!
Instead of terminating the script immediatly, throw an exception.

Imho if there is an exit() or die() statement somewhere in the script, it can be rewritten in a more maintainable/readable way. exit() and die() are like GOTO. 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.


include('header.php');

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

include('footer.php')

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!