Using PHP includes to easily edit your site’s static content!
delicious • digg • stumble • float • dzone • Print Article • comments rss
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Hey, in the following article I’ll show you how to add PHP includes to a static multi-page site, like a personal site or a portfolio, to help edit the site more easily.
For this article, I’ve presented a simple web page layout below. The template contains a header, a navigation, a place for the main content, a static sidebar, and a footer. If I were to add a link to the navigation, I would have to manually go through every page in the site and add a new list item.

However, if I switched to using PHP includes, I would only have to edit one file, which when edited automatically changes on every page. This helps when you need to change the link to a CSS file, add a couple of list items to a navigation bar, change your sidebar, etc.
So how do I do this? It may look a bit complicated but it’s actually only a few steps and you need to know absolutely no HTML or PHP. Here’s a nice little list for you:
- First, you have to change all of your site files from the .htm or .html extension to the .php extension. This can be done on your local computer or on your server, using an FTP program.
- Then, figure out what sections of the site are repeated and unchanged page-to-page. In the site template above they will be the header, the navigation, the sidebar, and the footer.
- Next, create separate files for each of these sections, for example header.php or footer.php, and add the code for each one in the correct file.
- Finally, take all of your old files and replace the previous sections of hard code with this line of PHP:
<?php require('header.php');?>This line of PHP code includes the chosen PHP file into the HTML code. All you have to do is copy the code above and replace the “header.php” with the name of your specific includes.
Once you’re done, your pages’ code should now have a layout similar to this:

As an extra addition, I’ll show you how to hide/show/change certain sections depending on the URL that you’re located at.
Edit: I’ve added a nice little .zip file that shows you a super-simple example on how to use these PHP includes on your site.
You can edit the above code adding lines for each thing you want as a condition, just changing the “index.php” segment to whatever your page url is called (like about.php, etc.) or the required include (like sidebarcontent.php, etc.). Have fun!
Once you’re done, you’ll have a nice easily-editable site. Enjoy!
Sweet, very simple very easy.
Nice. It took me a while to find the magic of php in my web career, but when i did I started with includes.
The only thing I would add to this would be that you said…
“First, you have to change all of your site files from the .htm or .html extension to the .php ”
This is not the case (well if you are on not running a windows server). You can add a couple of lines to a .htaccess file and upload it to your root directory and you can render .php within your .htm or html files (very handy, and hides what coding language you are using to others too)
Those lines are…
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
Thats it.
Can you post the above example in a .zip file?
This is a great tutorial. Thanks for sharing!
@Ash: Sure, you can cheat and do what you’ve suggested, but that’s considered extremely bad practice.
By doing what you’ve said, you’re instructing the server to parse all .html and .htm files using PHP - which adds unnecessary overheads when serving static HTML pages. What if those pages don’t contain any PHP? PHP will still try to parse them, unnecessarily.
Do things properly, don’t cheat
Why did you use require() instead of an include()?
Thanks for the tut! Simple n effective!
@ Kevin — The require() statement includes and evaluates the specific file.
require() includes and evaluates a specific file. Detailed information on how this inclusion works is described in the documentation for include().
require() and include() are identical in every way except how they handle failure. They both produce a Warning, but 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.
So what do you recommend using the most?
This is a real effective way of separating content from structure and making it easier on your clients to edit their content without having to sift through the layout code they don’t need to worry about. Write a simple program that loads the external .php files and lets you edit and save them and your on your way to a nice CMS!
@Kevin - Well I use mostly include. So If something happens and the “included” file is “missing” the script will continue.