Jump to content

Check out our Community Blogs


Member Since 28 Jun 2006
Offline Last Active Dec 19 2006 03:04 PM

#314556 TUTORIAL: Save time using the PHP include Function <?php include

Posted by Dan on 28 June 2006 - 06:08 PM

One of the main problems faced by webmasters is saving time when it comes to creating websites. When starting a website, one may choose to create a navigation menu like the following to link to each page:

<a href="http://www.yoursite.com/index.htm">Home</a> - 
<a href="http://www.yoursite.com/about.htm">About Us</a> - 
<a href="http://www.yoursite.com/links.htm">Links</a> - 
<a href="http://www.yoursite.com/contact.htm">Contact Us</a>

Which looks like: HomeAbout UsLinksContact Us

So now, you have your 4 pages - and all is well. But, as the site expands you need to integrate a forum - which leaves you with 4 pages to update. No problem at all... But supposing the site had 100+ pages! That would be a very monotonous time consuming job.

This is where we bring in the php include command to really speed things up!

Create just the navigation menu on its own, and rename all of your pages to .php

In this case, the navigation menu will look like this:

<a href="http://www.yoursite.com/index.php">Home</a> - 
<a href="http://www.yoursite.com/about.php">About Us</a> - 
<a href="http://www.yoursite.com/links.php">Links</a> - 
<a href="http://www.yoursite.com/contact.php">Contact Us</a>

Which looks like: HomeAbout UsLinksContact Us

Save this file as "menu.inc" as we will be including it elsewhere - hence the .inc extension.

Now, open up your .php pages that the menu links to, and simply add the following code at the very top, to include the navigational menu on the pages:

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

The code is telling the php page, to "include" the menu.inc file - and it does!

Once this has been added, any changes in the menu.inc file are reflected across the whole site making updating a lot easier!

Why stop at menus? This can be utilised for adverts, footers, headers, templates and much much more!
  • 2

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download