Jump to content

Does including (or requiring) a file execute it immediately?


DWilliams

Recommended Posts

Or is it all pieced together into one file then executed?

 

I ask because I'm thinking of a weird way to handle errors on my site that would integrate with the interface smoothly without much work on my part.

 

Every page on my site requires template_top.php at the start of the script and template_bottom.php at the bottom. I believe this is a pretty common thing to do, and it standardizes all my menus and interfaces and whatever else, plus it gives me a nice place to deliver messages to the user. What I want to do is something like this simplified example:

 

template_top.php:

<?php
// layout stuff here
try
{
?>

 

index.php:

<?php
require_once('template_top.php');
// a wild error appears!
throw new Exception('oh no!');
require_once('template_bottom.php');
?>

 

template_bottom.php:

<?php
}
catch (Exception $e)
{
   echo 'An error occured! Message: ' . $e->getMessage();
}
// layout stuff here
?>

 

So that's where my question comes in. Both template files contain code that, if executed on it's own, is invalid. Pieced together in my pages though, it forms correct code. I'd really like to do this since all I'd have to do if my script encounters a problem is throw an Exception and everything is instantly handled without me having to fight with display issues. If this isn't possible, does anybody have any better suggestions?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.