You are invited to Log in or Register a free Frihost Account!

Wonders of the include() in PHP.

I know a lot of people these days who don't know a lot about html/php etc use templates which they have either been given for free or paid for.

A lot of these are bog standard, and don't usually include any php!!

PHP Include

With the PHP include file, you can save time editing your templates by using a text file with (for example) your news, your menu bar etc.

Example: Menu Bar

<?php include(""); ?>
<li><a href='#'>Home</a>

When you add this to

<li><a href='#'>About</a>

You will automatically have "About" and its hyperlink added to your menubar. You can create by simply renaming a .txt file.

Add the <?php> tag above into all your templates where your menu bar is, and when you add a new page to the site, simply update with the new link :]

You can see examples of this at my site:

I use the include to update the "latest news" column, my menu bar and my networked sites page :]

EDIT - I forgot to add, anyone who views your source will not see the php tag, they will see what is actually inside on the source! Secure!

I'm not sure, but you can also make files hidden aswell. :]
Myself I hardly use it for my menu. In my new and upcoming layout the only thing which I actually include is the page's content itself. The header, sidebar and footer are all just placed on the index page.

When going to other pages the url's are basically index.php?page=bladiebla

Myself I find that a pretty convenient way of dealing with things. If I want to make minor adjustments it's all just the index file at any time which will have to be changed mainly. But well yeh, everybody has his/her own preferences when it comes to what to include.
i use include() alot myself just for additional functions that i may not want to include in the core processing file.

i find it helps organize things much easier than having a core processing page that is 1000+ lines of code long.
Stubru Freak
This is the way I'm using, where I include the surrounding.
At the start of every page you type

Then at the end of your page you include a template file:

Then in that template file you type:

Then where you want your content to appear you type:
echo $Content;

Except the advantage of not having to place the code of your whole page in every file, it has these other main advantages:
- You can easily set for example, a different title for every page by including a $Title="xxx"; variable on every page, and using $Title as title.

- You can even get your title from the page name or use a different menu for every page this way:
array_shift(explode(".", array_pop(explode("/", $_SERVER["PHP_SELF"]))));
// Note: doesn't work for filenames with dots, like:

- You can also set cookies or other headers, like redirects, while you are already generating output.
I use require(). I have heard that require() is faster than include(). I haven't noticed any difference between those, but I use require(), as I have already said, I have heard that it is faster. Do someone know about this? Is this really true, what I'm saying?
the only way require() would be faster if there was an error processing the file. the below is from

require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.