When creating a basic website most people create a standard design, usually with a navigation menu to the left, top or right. This is the best way to do it as shown by many successful websites. The only problem with using a navigation on every page that needs to have new links added to it often is when you have many pages, it can become a very time consuming operation.

In this article I am going to show you how to create a PHP Include. This will allow you to update your navigation pane from one single file that will update all the other pages at the same time. PHP includes are very easy to use and are useful for anyone with a static website.

How it works

The best way to think of a PHP include is as a window, imagine all your pages have this window and on the other side of the window or a frame. It’s like an iframe except it does all of the content injection on the server side.

I’m not sure that description makes sense, I’ll probably change that at some point. ;)

Anyway, on each page where the navigation is meant to be, you put a small PHP code snippet. This snippet points at a single file somewhere on your server. Imagine the single file was called “hello.php” and on your page “index.php” you have the PHP code to inject hello.php the content of “index.php”. If “hello.php” has the word “hi” in it, “hi” will appear in “index.php” as if it was normal text entered in “index.php”. Now, imagine what you can do when you put the php code on all of the pages of your site. You can put it on as many pages as you like and you can put anything inside “hello.php”.

For this excersise we’ll be putting some links in “hello.php” and including it in “index.php”.

Code

First, make sure you have created “hello.php”

You should already have your own site design, so I’ll assume you do. Now what you need to do is copy all of the links in your navigation menu to “hello.php” and save it. Make sure “hello.php” has NOTHING else apart from your links, no styles headers or anything. Not even HTML or BODY tags! Now save that file and put it on your server.

You’re almost done, all you have to do now, if go round and replace the navigation links with the following code:

<?php include('hello.php') ?>

If you want to put your includes in a sub directory (example.com/includes maybe?) then use this: (remember to replace includes with the name you gave your own sub directory)

<?php include('includes/hello.php') ?>

That is all you should need to do to get your navigation included. If you have any problems I will be more than happy to help you. Just leave a comment if you get stuck.