Introduction to using PHP Includes

When includes are used, the server puts the include file (which is simply a plain text file with a section of HTML code) together with the page before it's served to the browser. So if you look at the source code of the page, it looks no different than if the content were coded right on the page itself - which it is, except that the server is putting it there for you.

Benefit: When there's a section of text to be updated, only one single text file has to be updated and uploaded to the server and the change will show on all the pages it's on without having to touch them or upload them again.

Using PHP includes is similar to using SSI, but I find it easier to use PHP includes. For one thing, there are so many other things that PHP can be used for, and since often it isn't possible to use SSI and PHP on the same site, I'd rather use PHP so that I have so many more options available. Like automatically updating dates, which I've put on a page with PHP snippets.

Note: It's a good idea, to keep all of the includes in an /includes/ folder of their own so that they're easy to find. It's the same as keeping all the images in an /images/ folder as part of site organization.

To use them, you just put a simple line of code to "call" your include file in the html code at the exact spot on the page you want the text or navigation links to appear on the page in a browser.

This is what the code looks like if the file using the include is in the root (main) folder of the site:

<?php include ( '/design_development/includes/navigation.html' ); ?>

If the pages using the includes are in a /subdirectory/ this is what's used, the same way that's used for images:

<?php include ( '/includes/navigation.html' ); ?>

That's it!

Note that the file uses a .php file extension, which is the proper way to do it. Typically, pages that use PHP also have a .php file extension, but a simple entry in your .htaccess file will tell the server to parse (process and read) pages for PHP that have an .htm or .html file extension. So if you already use one of those, there's no reason to have to change.

What I generally do is put my page together with whatever design I'm using, and when I'm sure it's all how I want it to be, I move elements that will be on a lot of pages (or all of them) into a text file and replace with the line of code for the include.