[web-tips > includes]

The wonderful world of "includes" by tedd

The most important technique I found in doing web development over the last 23 years has been knowing how to use the "include()" statement. If you can grasp how to use php-includes, then it will simplify your coding considerably.

For those who don't know what an include() is, or those who think they do, but don't use it -- then I say if you follow and understand what I provide here, you will change the way you work forever! It's that important for you to know what the include() statement is and how to use it.

First, you don't have to be a programming wizard to use it -- it's very simple. The statement simply allows you to include redundant code.

For example, let's say you have a menu that spans several web pages -- instead of repeating that code in every page, you create a menu file (menu.php) and then "include" it like so:

<?php include('menu.php'); ?>

You place the above statement where your menu code would have been and automagically the include will be loaded when your web page is loaded. That's mondo cool.

This makes it great for sections of code that are repeated in other pages, like the header, menu, and footer. You keep/edit/maintain just one file regardless of how many times it may be needed elsewhere.

Most of my pages begin and end like so:

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

... html...

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

My header.php file has all the stuff from the DOCTYPE to the start body tag AND has it's own include to include the menu. So includes can include includes.

To read more, you can review this:

PHP Includes

The only real difference between before/after you do this is that you must have all your files end with ".php". I can provide a file that will change the way the php interpreter will look at files, but it's easier to just change the suffix. Besides, that's what all the wizard programmers do anyway.

If you want an example of code to review -- please note the below two sites look exactly alike, but they are coded considerably different:

Example without Includes

Example with Includes

The code for both sites can be found as follows:

Code for Example without Includes

Code for Example with Includes

One parting note, these examples also show a reason why one should switch from using image roll-overs to css roll-overs -- it's much easier to change things, plus it loads quicker.

In any event, if you don't currently use the include() statement, then studying these two examples will alter the way you code forever.

Please donate $5 below for my development effort -- thank you.

This technique W3C validates under both HTML 4.01 Strict and CSS guidlines.