by: Mark Alexander Bain
One of the most important parts of building any web site is ensuring that all the web pages that it contains have a consistent look and feel. For example each page may require:
- the same header to be displayed
- the same footer to be displayed
- sames links to be displayed down the side of the page.
This can, of course, be done by editing the HTML in each page (which is time consuming) or by using frames (which is not particularly popular). However, it can be also achieved by using just a few lines of PHP code.
A web site programmer is able to use PHP to create a dynamic web page and they have an number of techniques at their fingertips that may help them in making the web site contents consistent. For example they may:
- use the include method to load a header and a footer file
- process any web page to give the same look and feel.
Of course, all that the web site user will see is a set of web pages all with the same format – all with the minimum of time and effort for the programmer.
Using the PHP Include Method
The include method is normally used to load a library for use in a PHP application. However, the same technique can also be used to load one or more HTML files – one which, for example, could contain a standard header and footer:
The contents of the two files to be included (header.html and footer.html) will now be displayed when the PHP file is viewed via a web browser (such as Internet Explorer or Firefox), and if the two lines are added to any other PHP files then they too will display the same header and footer. Another advantage to this is that if the header and footer files are changed then the PHP will reflect this with no additional work by the web site programmer.
Processing an HTML File with PHP
Instead of including the include statements for the header and footer files in each PHP file the programmer may prefer to create individual HTML files and then process them with a single PHP file:
In this case the file to be processed (article.html) is recorded as a session variable and then this is passed to a second file (body.php) which will display the file’s contents in an appropriately formated page:
Again the programmer will only need to edit the one file (body.php) in order to make changes that will be reflected in each page on the web site, thereby minimizing the programmer’s time and effort.