PHP is a cross-platform, server-side, HTML embedded scripting language devised by Rasmus Lerdorf in 1994. Let's see what we mean by that.
It seems that all programming courses start off with the obligatory 'Hello World!' program, so here is ours! (Explanations are given below.)
1<html> 2 <title>Hello PHP</title> 3 <p> Here is the obligatory "Hello PHP" script!</p> 4 <? 5 print ("Hello PHP!!"); 6 ?> 7 <p>Now we can get on with learning about PHP!</p> 8</html>
print() is a function which prints strings to a browser. In fact we will be usinf echo for most of this course which does the same thing.PHP scripts are embedded in HTML using the following symbols.
<?
//.... PHP script goes here
?>
//or
<?php
//.... PHP script goes here
?>
The first program didn't do much! Here is a program that actually does something useful, useful that is if you are interested in the value of Greenwich Mean Time!
1<html> 2<title>Hello PHP</title> 3 <p> The date and time in Greenwich, England is 4 <? 5 $myDate=gmdate("Y-m-d H:i:s"); 6 print($myDate); 7 ?> 8 (This is given in International format)</p> 9</html>
$myDate.In order to understand how php works, it is necessary to understand how html works! When a client (i.e. usualy a browser running on a PC or Mac) makes a request to a server, provided that the file can be found, an HTML stream is sent down to the client, which uses it to display the web page. In order to create this HTML stream, the web page is read from top to bottom by the server. This process is shown graphically in this picture.
What happens when a PHP page is requested? Well on a PHP savvy server essentially the same process takes place. However when the server gets a page with the extension .php, it is put on high-alert that the page will probably contain code that needs to be interpreted. Again it will read from top to bottom, but when it comes to the sybol <? it will know that everything between this symbol, and the closing symbol ?> will be PHP script, and will need to be sent of to the servers PHP engine for interpretation. The PHP engine takes the script and converts it into an HTML stream. This is shown graphicaly in the following illustration.
In fact all that happens in Example 1, is that at line5 the string "Hello PHP!!" is added to the HTML string using PHP's print() method.
Line 5 is what is known as a 'statement' in programming jargon. In other words it is a set of instructions that does something. All statements in PHP need to end in a semi-colon (;). This is not optional (as it is in javaScript), if you omit the ; you will get an error!
Example 2 is a little more interesting.
In the first example we just asked our engine to add a string to our HTML stream. In the second example we are asking the engine to do some serious work!
In line 5 we use the gmdate() method to find out what the time is in Greenwich Mean Time (There's nothing magic about this, this method uses the servers clock to calculate GMT), and we read it into a variable $myDate. We then print it out. The string taken by the gmdate() method is a formatting string.
That is rather a lot of new information! Don't worry. We will be going into all this in latter lessons.
The main thing to take from this lesson is that PHP is an embedded script that is interpreted on the server by PHP savvy servers. Such a server will read the script from top to bottom, and when ever it comes across PHP script (identified by an opening <? and a closing ?> it will send it off to the PHP engine to be interpreted and converted into HTML, that will be inserted into the HTML stream generated by the HTML page.
It is this stream that will be sent to the client resulting in a display.
©Frank Boumphrey 2001