Class 7 – The Main Uses of PHP: Templating and Business Logic
Posted: July 19th, 2009 | Author: amos | Filed under: php | Tags: class 7 | No Comments »PHP is the server-side programming language we will use for the remainder of this course. PHP is a general-purpose language that can be used for many things. However, parts of the language have been especially designed to be easy-to-use in web development. The primary uses of PHP in web development are:
- templating
- business logic
Templating
Templating is the process of distilling the component parts of a web page, or a set of web pages, such that there is as little redundancy between pages as possible. This is easily illustrated with an example.
The BBC News website uses the same header section on every single page:
Every page on the BBC News site also has the exact same left navigation section:
… and every page on the BBC News site has the same footer at the bottom:
Given that there are thousands of pages within the BBC News website, if the website managers decided to add a new featured link to the header, they might have to modify the top section on 10,000 seperate XHTML pages. Clearly, this can become unmanageable, even for a much smaller site.
Any web developer with a modicum of experience would immediately realize that a site such as this should be broken down into templates. Most likely, the XHTML code for the top header section is stored in its own file, isolated from the rest of the page. Let’s say this file is called “_header.html”. Every page on the site includes “_header.html” into the top section of the XHTML for that individual page. If the site administrators want to add a new link to the top header, then they edit only “_header.html”, and all the pages on the site automatically show that update, since they are all loading the header XHTML code from that file.
The same process would occur for the left navigation section, footer, and any other elements that repeat themselves across the site.
Business logic
The second most important use of PHP is to implement what is called “business logic.” This consists of the logical rules necessary to determine what content should be shown on any given page.
For example, when a user goes to bing.com, and enters a search term in the form, you know enough to realize that this is probably an XHTML page with CSS style sheets, and a <form> tag with a <input type=”text” …> text input where users enter in the term they want to search. When you type a search term into this form, such as “web development intensive”, and click submit, you are taken to a new page with the search results. Somehow, this new page knows what you typed in the form on the last page, and has gone and retrieved a list of sites that match the search term “web development intensive”, presumably from a giant directory of websites somewhere.
The search results page is presumably templated. Each search result page looks pretty much the same. The header, footer, left nav, and right nav sections of the page look pretty much the same no matter what you search for. It’s really just the exact details of the search results that differ from one search to the next. It’s safe to assume that this page is templated, and the details of exactly what search results are shown on any given search results page are dictated by certain business rules. The logical rules determine what content gets put into the template.
This page has used business logic rules to infer what it was you wanted to do on this site, and has retrieved the relevant content from a database somewhere and shown you the search results in well-formatted (presumably templated) XHTML code. Any e-commerce site, news site, search engine, classifieds site, or any other type of site with dynamic content that is stored in a database will most likely operate the same way.
No related posts.

Leave a Reply