Tutorial: Easy PHP Website Templates

Tutorial: Easy PHP Website Templates

This tutorial will convince you how to build your PHP website templates. Includes a free sample PHP website template that may be used in your project.

This tutorial will guide you step-by-step through designing your first PHP website template. Though this tutorial is designed for readers with little or no PHP experience, excellent working knowledge of HTML is required. Readers desiring to implement the solutions presented in this tutorial should have access to either:

  1. A test machine with a current version of PHP installed. Download a free copy of PHP from PHP.net to test your PHP website templates.
  2. A web server that supports PHP scripts.

The Components of a PHP Website Template

Before you delve into the code, it’s essential to understand the basic components of a PHP website template and its functions.

Most PHP website templates contain at least three essential parts (though some may include more):

 

  1. a header,
  2. some content, and
  3. a footer

The header portion of a PHP website template contains primarily static content that is of high importance. Examples of information that might be found in a header include company logo/branding, artistically styled titles, site navigation, etc. The header of a PHP website template is consistent throughout all pages of the website.

The content portion of a PHP website template is the portion of the page that changes as you navigate through the website. It contains the ‘meat’ of your site and is the part most likely to change most frequently.

The footer portion of a PHP website template contains mostly static content but is typical of lower importance than that found in the header content. Examples of information that might be found in a footer include trademark and copyright notices, contributor credits, etc.

Armed with the knowledge that some components in PHP website templates are static and consistent across all pages. In contrast, other features change frequently and are unique to specific pages, you are ready to build your first PHP website template.

Building Your First PHP Website Template

Start by building a simple HTML framework for your PHP website template. Based upon the information given to you above, find the logical separation in your code for your header, content, and footer components. Then split that framework into three separate documents named header.php, content.php, and footer.php. Your completed work must look something like this:

HEADER.PHP FILE:

< html >< head >< /head >< body >< div class=”header” >HEADER GOES HERE< /div >

CONTENT.PHP FILE:

< div class=”content” >CONTENT GOES HERE< /div >

FOOTER.PHP FILE:

< div class=”footer” >FOOTER GOES HERE< /div >

You’re almost done, but before your PHP website template will work, you need to tell PHP to include the header.php and footer.php files in your index.php file. This is done by calling the built-in PHP include(); function, as demonstrated below:

CONTENT.PHP FILE:

< ?php include ‘header.php’;? >< div class=”content” >CONTENT GOES HERE< /div >< ?php include ‘footer.php’;? >

Tips and Tricks for Your PHP Website Templates

Having trouble getting your PHP website template to work? Don’t despair! Unlike HTML, PHP can be an unforgiving language. That said, it’s a powerful tool to have in your toolbox as a developer and well worth the time you will spend in acquiring it. There are a few simple things you can do and check for when your scripts aren’t working as expected:

  1. Are your lines of PHP code adequately terminated? Almost all lines of PHP code end with a semi-colon. Failure to properly complete your code will result in errors.
  2. Did you properly encapsulate your PHP code with tags, as demonstrated in the final content.php script above? Failure to properly encapsulate your tags can cause lines of PHP script to show up on your HTML page or cause your hand to fail.
  3. Are you using absolute URLs when calling the PHP include() function? For security reasons, most web hosts running newer versions of PHP have chosen to disallow the use of absolute URLs in calls to the PHP include() function. Try using relative URLs instead. Don’t know how? There’s an excellent tutorial on relative URLs available at WebReference.Com.
  4. If you’re using a simple text editor (such as Notepad), you will find a line counter invaluable in debugging your scripts. Ult-Tex.Net offers an excellent free online counter on their site: Line Counter for Website Development.

Fine Tune Your PHP Website Template

Whew! You’ve performed it to the intention of this tutorial. By now, you should have your functioning PHP website template (even if it is still just a diamond in the rough).

 

 

 

Leave a Reply

Your email address will not be published.