Modular Websites

Includes

You only learn something if it is truly useful. In that way, PHP provides a very powerful function to modularize the website with a bare minimum of code: PHP include. Include takes a file name and simply inserts that file's contents into the script that issued the include command.

This means that you can create a common menu file that you want all your web pages to include. When you add a new page to your site, for example, instead of having to update the links on several web pages, you can simply change the one file. To create a common menu file that all our pages will use, save a blank file as "navigation.html" in the same directory as the index.html and paste in the nav element. We then plug that menu into our index page, replacing the nav element with the following code:

<?php include("navigation.html"); ?>

That's it. When a browser makes a request for your index page, it will include the code from your navigation.html file. And you can do this with the header, the footer, and any other parts of your web site that remains the same throughout the web site.

The example I've prepared does exactly that. The one thing I added was an ability to give the user feedback on the page they are on by adding both an id to each page and a unique class to each menu, and when they coincide, the menu lights up to show the current page.

Visitors do not see the PHP code. All the PHP is processed out. What remains is HTML, as if it had been written as one HTML document.