Enhance your site with PHP includes

An external style sheet allows all of a site’s styles to be controlled from one point—which makes for very efficient page layout and design.

PHP includes” can be used in a similar way, in that elements appearing on multiple pages can be controlled from one location. For example, it is common to have a menu on every page of a website. If you want to update that menu—such as adding a new menu item—it will be a nightmare to do this if you have to open every page on your site to alter each instance of that menu.

Luckily, you do not need to know much about PHP to use PHP includes. There are various ways to use them, but what follows is a very simple method that works very well.

Step one

Let’s assume that you are creating an include file for your site’s main menu.

Firstly, create a new directory (or folder) on your website, and call it something like “includes”. (For this example, it is assumed that this directory is located in your root folder—in other words, that the directory is located at yoursite.com/includes/.)

Inside this directory, create a new file called menu.php. (It is not essential to use the .php extension here—you could use .html, for example—but it’s worth doing so, as I’ll explain later.)

Now, copy the menu code from one of your pages and paste it into the menu.php file. Here, for example, is the (somewhat simplified) code for this site’s menu (which I keep in a single include file):

<ul id="menu">
	<li><a href="/">Home</a></li>
	<li><a href="/web/">Web</a></li>
	<li><a href="/words/">Words</a></li>
	<li><a href="/services/">Services</a></li>
	<li><a href="/about/">About</a></li>
	<li><a href="/contact/">Contact</a></li>
</ul>

Upload menu.php and its directory to the web.

Step two

Ideally, you would have set up your system of includes while building your site. If your site has already been created, you will have to do a bit of manual labor; but once you’re done, you will never have to do this again.

For PHP to work on a web page, the page needs to have a .php extension, rather than .html. That’s the only real bad news if your site is already set up with .html pages. (However, if this is your situation, don’t despair, as there is a simple way to allow PHP to run on .html pages by means of a .htaccess file.) When I build sites now, I never use .html any more, but rather .php—regardless of whether or not there is any PHP on the page. (It will make life easier in future if I need to add some PHP.) When a page on the internet is requested by a browser, the server that hosts the page will check to see if there is a .php extension, and if there is, it quickly runs the page through a PHP parser (which carries out any PHP instructions in the page) before serving the page to the browser. For our menu example, we want the PHP parser to include the menu.php code on the requested page before it is sent to the browser.

So, back to the setup. On every page where the menu is to appear, remove the menu code and replace it with this:

<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/menu.php"; ?>

It’s as simple as that! The beauty of $_SERVER["DOCUMENT_ROOT"] is that you can use the same code on every page of the site without having to change the path to the menu.php file.

Final notes

In the example above, I just placed the actual menu code in menu.php, but you can actually place as much code into an include file as you like—such as the containing div etc. I tend to place any code that will appear throughout the site into a PHP include, so that it will be easy to edit from one location in future. DOCTYPE, CSS links, header sections, footer div… You name it, it’s in an include.

You can, of course, place PHP includes within PHP includes, which is why I use the .php extension for my include files—just in case.

As I said, you don’t need to know much about PHP at all to use PHP includes. The code that tells the PHP parser to do something is the <?php ... ?> bit. The parser will execute whatever instructions are found in between those opening and closing tags. The only other important thing is to give the page a .php extension. When you rename your pages with the .php extension, make sure to update any links that reference the .html version. (It is also good to delete the .html versions of each page from the server if you are uploading pages from an FTP program, as both the .php and .html versions will be sitting on the server. Make sure your .php pages are working nicely, and links all updated, before deleting the .html versions.)

  • Twitter
  • Digg
  • StumbleUpon
  • Facebook
  • Ping
  • Mixx
  • DZone
  • Google
  • DesignBump
  • DesignMoo
  • DesignBuzz
  • Delicious
  • Reddit
  • Technorati
  • Evernote
  • FriendFeed
  • Email

Acknowledgements & Links


Leave a Comment

Feel free to comment. If you can improve on this post, your comments will be included, along with your name and a link to your website. (Comments are moderated, so spammers… just don't bother.)


What Now?

Search all Entries | Ebooks by Books for Learning | Services