Class 7 – Introduction to PHP

Today we enter the realm of server-side technologies.  PHP is the server side scripting language that we will be focusing on, but many of the high-level concepts we will be exploring with PHP could just as easily apply to other server-side languages like ASP.NET, Ruby, Python, Java Server Pages, and others.

What is PHP used for?

You may wonder why we even need server-side languages, given the we have seen how the vast majority of web pages are created with a combination of XHTML, CSS, and Javascript.  Use Firefox to View -> Page Source on any web page and you will probably see XHTML, CSS, and Javascript code, but absolutely no PHP.

Web developers use XHTML, CSS, and Javascript to control how content shows up in the web browser and how the user interacts with that content.  That’s the sole purpose of client-side technologies.   Server-side technologies, like PHP, are used to control how that content is assembled and managed on the server before it has even been sent to the browser.  Thus, PHP is used to dictate what is often referred to as the “business logic” of a web site.

There are three main uses of PHP:

  1. web page templating
  2. storing and retrieving content
  3. tracking user sessions

These three uses are not mutually-exclusive.  For example, templating and tracking users depends upon the ability of PHP to store and retrieve content.

Web page templating

Most web sites have elements of the page that are repeated on each page.  Very commonly, global navigation or other important navigation elements are repeated on all pages of a site.  For example, Google uses the same global navigation links across many of its sub-sites. Notice that the global navigation remains mostly the same from page-to-page, with slight changes:

Global navigation of Google home page

Global navigation of Google home page

Global navigation of Google home page with logged-in user

Global navigation of Google home page with logged-in user

Global navigation of Google Images with logged-in user

Global navigation of Google Images with logged-in user

Global navigation of Google News with logged-in user

Global navigation of Google News with logged-in user

It is very unlikely that Google engineers have hand-coded each of the pages on the Google site individually to have the same XHTML and CSS code for this top section.  If this were the case, and Google decided to make a slight change to the layout of this top navigation section, the engineers would have to go through the code of each one of the pages of all these Google sub-sites, and change the XHTML accordingly.  This would be a very inefficient and expensive way of maintaining a site.

It is much more likely that the engineers have coded this top section once, and are reusing that same code on each of the pages, with slight variations.  If they then wanted to make a change to this top section across the site, they would change that one bit of code, and the rest of the site would update automatically.  This would be referred to as being a template.

Take another look at the websites you frequently visit, and you will probably see that there are many sections of content that are repeated on each page.

Storing and retrieving content

When you visit many sites, you will see that the content is updated regularly but the design often does not change.  For example, news sites, e-commerce stores, and blogs all have regularly udpated content (news, products, and blog posts, respectively), while maintaining a consistent design.

Every time a new product is added to a store, the web developers do not usually have to hand-code the new product information.  Rather, they build a site that allows an administrator to enter the details of a new product into a form, and when they click submit, that information is stored in a database.  And each time a consumer visits the e-commerce site, the server-side code retrieves the latest list of products from the database and fits them into a web template.

As an example, consider the products on Amazon.com.  The listings of Sheets and the listings of Comforters look identical in layout and design, however the products that are listed in those two sections are different.  This is because the data is being pulled from two different data sets in a database – one for sheets, and one for comforters, but the data in each case is being fit into the same template.

Amazon.com Sheets

Amazon.com Sheets

Amazon.com Bedding

Amazon.com Comforters

Thus, the storage and retrieval of data is critical for any dynamic web site.  And this use of server-side scripting generally goes hand-in-hand with the templating of web pages.  In this case, the template would control the general layout and design of the products section of the site.

Data can be stored and retrieved to/from a variety of different places

  • databases, which store data in a way that is conceptually similar to spreadsheets
  • files, such as text or image files
  • web services, which are a mechanism that allows one server to store and retrieve data from another server
  • cookies, which are small bits of information that a server can store temporarily on the client computer

PHP can be used to handle the details of using each of these storage mechanisms.

Tracking user sessions

It is often desirable for a web site to be able to track visitors.  For example, a user who logs in to their webmail account expects to see their own mail and not someone else’s.  Thus, in order for the webmail web server to properly determine which user is currently using the site, it must track each visitor to the site in some way in order to differentiate between multiple users visiting the site at the same time.

Sometimes you want to track users even when they haven’t logged into your site.  For example, if you were giving a tutorial on your site, you may want to know how far each user has gotten in the tutorial, even without requiring them to log in.

Tracking users usually involves using cookies at the very least.  Often, it will also involve the other sorts of data storage as well.

Related posts:

  1. Class 7 – Introduction to Arrays in PHP

Tags:

Leave a Reply