FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Wonders of the include() in PHP.





Sephz0r
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

Code:
<?php include("menu.inc"); ?>


menu.inc
Code:
<li><a href='#'>Home</a>


When you add this to menu.inc:

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


You will automatically have "About" and its hyperlink added to your menubar. You can create menu.inc 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 menu.inc with the new link :]

You can see examples of this at my site: http://projectfreedom.frih.net

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 menu.inc on the source! Secure!

I'm not sure, but you can also make files hidden aswell. :]
DX-Blog
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.
BearClaw
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
Code:
ob_start();

Then at the end of your page you include a template file:
Code:
include("includes/template.php");


Then in that template file you type:
Code:
$Content=ob_get_clean();

Then where you want your content to appear you type:
Code:
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:
Code:
array_shift(explode(".", array_pop(explode("/", $_SERVER["PHP_SELF"]))));
// Note: doesn't work for filenames with dots, like: foo.bar.php


- You can also set cookies or other headers, like redirects, while you are already generating output.
SamiTheBerber
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?
BearClaw
the only way require() would be faster if there was an error processing the file. the below is from php.net

Quote:
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

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.