Includes

Admittedly, using PHP to print the message "Hello World" isn't a particularly useful application for PHP. We can accomplish the same thing by just writing the message in our HTML. However, since you now understand the basics of PHP, we can move on to using PHP for something useful.

In this section, we will use the PHP include function to access a header and footer saved in independent files and add them to the beginning and end of our PHP file, respectively.

Creating the Header and Footer

When creating large sites, developers may sometimes find themselves putting the same information in a number of files. This often happens when a site has a header, footer, or even a menu that is the same on every page.

If this information could be stored in a single file, and then accessed by the rest of the pages that require it, then site wide changes to this information become incredibly easy and fast to implement. All a developer would have to do is update the file that is being called upon by the rest of the files in the site.

With PHP, we can do this quite easily.

Including the Header and Footer

Now that we have a header and footer, we are ready to include them in a document. We'll go ahead and add them to the top and bottom of the following content using the PHP include function.

<p>
I, Dr. Giggles Magee, am an evil scientist bent on destroying the world.
</p>

<p>
I lead an army of smurf warriors, who, at my request, sing songs to me as I sleep. They also bake cookies for me on a regular basis, but that isn't important.
</p>

<p>
I really like cookies.
</p>

Amending a Problem with this Method

One disadvantage to using this method is that every document using this header is going to have the same title, that is, the text inside the HTML title tags (<title>...</title>), is going to be the same. As it is important to have descriptive titles, we need to find a way to fix this problem.

Don't worry though, be confident with the knowledge you have gained so far, and we will be able to work out a solution in no time.