Home » Programming » How to Create a Consistent Web Site with PHP, Using PHP to Display and Format Web Pages

How to Create a Consistent Web Site with PHP, Using PHP to Display and Format Web Pages

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:

<h1>How to Build a Consistent Web Site</h1>
<p>Each PHP file will need to start with the code above and end with the code below…</p>

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:

$_SESSION[‘page’] = “article.html”;

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:

<td width=10%></td><td>
</td><td width=10%></td>

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.

The copyright of the article How to Create a Consistent Web Site with PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Consistent Web Site with PHP in print or online must be granted by the author in writing.
PHP Programming, Mark Alexander Bain
HTML Code to be Included in a PHP Page, Mark Alexander Bain
Starting a PHP Session, Mark Alexander Bain
Using PHP to Format a Web Page, Mark Alexander Bain
The End Result, Mark Alexander Bain