This tutorial will take you step-by-step through creating your first PHP website template. Though this tutorial is designed for readers with little or no PHP experience, a good working knowledge of HTML is required. Readers desiring to implement the solutions presented in this tutorial should have access to either:
- 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.
- A web server that supports PHP scripts.
The Components of a PHP Website Template
Before you delve into the code, it’s important to understand the basic components of a php website template and their functions.
Most PHP website templates contain at least three basic parts (though some may include more):
- a header,
- some content, and
- a footer
The header portion of a PHP website template contains mostly 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 through all pages in 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 typically 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, while other components change frequently and are unique to specific pages, you are ready to begin building 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 finished work should look something like this:
< html >< head >< /head >< body >< div class="header" >HEADER GOES HERE< /div >
< div class="content" >CONTENT GOES HERE< /div >
< 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:
< ?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 invest in learning it. There are a few simple things you can do and check for when your scripts aren’t working as expected:
- Are your lines of PHP code properly terminated? Almost all lines of PHP code terminate with a semi-colon. Failure to properly terminate your code will result in errors.
- 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 in your HTML page, or cause your script to fail entirely.
- 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. Learn how to use relative URLs in your PHP Website Templates.
- 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 made it to the end of this tutorial. By now, you should have your own functioning PHP website template (even if it is still just a diamond in the rough).