Monday, 3 May 2010

Server-Side Scripting and PHP

In this screencast we will cover the basic concept of server-side scripting and use PHP to create a practical example of how the process works.

A web server works in much the same way as a restaurant - it receives requests, processes them, then sends the result back. It is important to note that the PHP script is not downloaded from the server, only the resulting output is. To go back to the analogy, once you have placed your order, the waiter brings your order to the kitchen at which point the chef prepares your food, the waiter then returns with your food - not the individual ingredients. To put that in terms of a LAMP (or Linux, Apache, MySQL, PHP) server:
  • Linux is represented by the restaurant - the server platform that brings all the parts together
  • Apache is represented by the waiter - the mechanism that receives requests and serves results
  • MySQL is represented by the kitchen's store room - the area where information is held pending use
  • PHP is represented by the chef - the script that processes the request based on logical instructions, pulling information together and preparing the result
It is important to note that this analogy falls short when accounting for how the result is displayed. PHP does not have a native front-end and relies on other web languages for structure and/or style. To use another analogy, the human brain is responsible for controlling the body, however it would fall flat without bones and would be shocking without skin and clothes. The brain, bones and skin represent PHP, HTML and CSS, respectively. This process will be summarised in the following exercise.

Exercise

Our objective is to display a heading that reads "Hello World!" on a web page, as you would using static HTML. The catch, however, is that the text must be output from PHP. The end result should be similar to what you see on screen now.

We will start by opening supporting file "helloworld_start.php" which has been set up as a static page. Since we only want to affect the contents of the h1 tag we can add our opening and closing PHP tags inside of it. For clarity, I'm going to remove the text that was there already. In PHP you need to "echo" things for them appear in the output. Since we want to echo text, also known as a string, we need to wrap it in quotes so that PHP interprets it correctly.

If you view your file through a local or live server that can process PHP, you should see the desired result.

Screencast URL : http://screenr.com/vGC

No comments:

Post a Comment