The server-side operation of PHP means that scripts are typically used to produce the content of the web page itself as well as any other work that needs to be done, such as accessing information in a database. So, the PHP equivalent of JavaScript’s ‘document.write’ is very common.
Before we look at some examples of script, read the brief introduction to PHP provided by John Coggeshall’s (2001a) guide to ‘Basic PHP syntax’.
Coggeshall’s (2001a) guide provides a very short introduction to how PHP is embedded in HTML and also introduces basic variables in PHP.To ensure you understand how to embed PHP in a web page, you could try out some of the sample scripts you have seen, on your own software platform if you have set one up or using your OU server account on the TT284 server. If you are not confident to do this now, you will learn how to do it in Activity 5.
Just as HTML comments, which are contained within special tags, are ignored by the web browser, the PHP parser recognises and ignores PHP comments in any of the three following styles:
// This is a basic one-line PHP comment
/* This is a C-style PHP comment that can span multiple lines. */
# This is a "shell-style" PHP one-line comment
The ‘C-style’ comment has the same format as a comment in the C programming language and the ‘shell-style’ comment has the format of a Linux or Unix comment in a shell script. The names are not important here, just that you have the option to use whichever style you prefer. Most PHP scripts you encounter will use a combination of the first two.
It is important to realise that comments are only useful if they are clearly written. Incorrect or misleading comments are not helpful. Comments also need to be maintained, such as updating whenever the code they document is changed.
We will be moving on to examine how to process and store the form’s data. You should become familiar with how PHP is embedded in an HTML page and how it is processed to produce additional ‘dynamic’ parts of a an HTML page.
echo 'Hello World from PHP';
First here is an HTML file which will serve as the starting point for the PHP file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Hello World</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body> <h1>Hello World: My First PHP Program</h1> </body>
</html>
<?php echo "Hello world from PHP"; ?>
Then edit the PHP file so that it writes out your name and OUCU and upload it again to your server account. Note that uploading a file with the same name as one already on the server will overwrite the file on the server.
If you encounter difficulties, post your sample PHP script and a description of your problem in the practical activities forum.
Name | Time |
---|---|
N. Heap | 1.20.32 |
K. Kear | 1.10.09 |
J. Jones | 1.30.07 |
$times = array("1.20.32", "1.10.09", "1.30.07");
This array has three elements which are implicitly number 0, 1 and 2. In PHP the array elements can be accessed using:$times[0]; $times[1]; times[2];
In PHP each array element can be given a ‘key’, which is an integer or a string, so the $times array could equally be declared:$times = array( 0 => "1.20.32", 1 => "1.10.09", 2 => "1.30.07" );
Each time in the array now has an explicit key (‘0’, ‘1’, ‘2’) but as keys can also be strings, we can set up an array of times where the keys are the names of the runners:
$times = array(
"N. Heap" => "1.20.32",
"K. Kear" => "1.10.09",
"J. Jones" => "1.30.07"
);
$times[‘N. Heap’];
The usual approach to processing an array is to use a loop. In PHP there are several forms of loop (e.g. for, while, etc., (PHP, 2012b)). A very commonly used loop is the ‘foreach’ loop which provides access to each value in an array sequentially:foreach (array_expression as $value) 'some PHP statement'
It can also be used to access both the key and the value of each element in an array:foreach (array_expression as $key => $value) 'some PHP statement'
Using ‘foreach’ the $times array (keys and values) can be accessed and written out as part of an HTML table:
// output table tag and header row first:
echo '<table border=1><tr><th>Field</th><th>Value</th></tr>';
// for each array element write the key and value as table data:
foreach ($times as $name => $time)
{
echo '<tr><td>';
echo htmlspecialchars(print_r($name, true));
echo '</td><td>';
echo htmlspecialchars(print_r($time, true));
echo '</td></tr>';
}
echo '</table>';
Name | Occupation |
---|---|
K Smith | Plumber |
N Jones | Electrician |
P Ibbotson | Plasterer |
$employment = array(
"K Smith" => "Plumber",
"N Jones" => "Electrician",
"P. Ibbotson" => "Plasterer"
);
asort($employment);
The asort function sorts an array based on the values found and maintains the associated key values. If you try out ‘sort’ you will find that it reassigns the keys (0, 1, etc.) and so the original keys are lost.Visit the excellent PHP website (PHP, 2012a) and browse the site to see what’s available. Read John Coggeshall’s (2001b) ‘An introduction to PHP’ as well as the ‘Introduction’ on the PHP (2012) website.
Note that there is an extensive ‘PHP Manual’ for reference on the PHP website and a ‘PHP Tutorial’ on the w3schools website (www3schools, 2012), which will be a useful resource throughout the remainder of this block. You can download an up-to-date version from the website.
There are also websites with various other resources, like tutorials, articles, or even podcasts. For example the PHP Builder (2012) and the Zend Developer Zone (2011) websites.
Coggeshall, J. (2001a) ‘Basic PHP syntax’, available at http://www.onlamp.com/pub/a/php/2001/03/08/php_foundations.html [accessed 2 February 2012].
Coggeshall, J. (2001b) ‘An introduction to PHP’, available at http://onlamp.com/pub/a/php/2001/02/22/php_foundations.html [accessed 02 February 2012].
IIS (2011), available at http://www.iis.net/ [accessed 02 February 2012].
MSDN Microsoft (2012) ‘Overview of the .NET framework’, available at http://msdn.microsoft.com/en-us/library/zw4w595w(v=vs.71).aspx [accessed 02 February 2012].
MSDN Microsoft (2012) ‘ASP.NET overview’, available at http://msdn.microsoft.com/en-us/library/4w3ex9c2.aspx [accessed 02 February 2012].
Oracle (2012) ‘Why MySQL’ available at http://www.mysql.com/why-mysql/ [accessed 02 February 2012].
Oracle (n.d.) Java, available at http://www.oracle.com/us/technologies/java/index.html [accessed 02 February 2012].
Perl (2012) The Perl Programming Language, available at http://www.perl.org/ [accessed 02 February 2012].
PHP (2012a) ‘PHP manual’, available at http://www.php.net/manual/en/index.php [accessed 02 February 2012].
PHP (2012b) ‘Control structures’, available at http://www.php.net/manual/en/language.control-structures.php [accessed 02 February 2012].
PHP (2012c) ‘htmlspecialchars’ available at http://www.php.net/manual/en/function.htmlspecialchars.php [accessed 02 February 2012].
PHP (2012d) ‘Function reference’, available at http://www.php.net/manual/en/funcref.php [accessed 02 February 2012].
PHP (2012e) ‘Arrays’, available at http://www.php.net/manual/en/book.array.php [accessed 02 February 2012].
PHP (2012f) ‘Include ()’ available at http://www.php.net/manual/en/function.include.php [accessed 02 February 2012].
PHP (2012g) ‘Superglobals’ available at http://www.php.net/manual/en/language.variables.superglobals.php [accessed 02 February 2012].
PHP Builder (2012), available at http://phpbuilder.com/ [accessed 02 February 2012].
Python (2011) Python Programming Language, available at http://python.org [accessed 02 February 2012].
The Apache Software Foundation (2012a) Apache Tomcat, available at http://tomcat.apache.org/ [accessed 02 February 2012].
The Apache Software Foundation (2012b) ‘Apache: HTTP server project’, available at http://httpd.apache.org/ [accessed 02 February 2012].
TIOBE Software (2012) ‘TIOBE Programming Community Index for January 2012’, available at http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html [accessed 02 February 2012].
w3schools (2012) ‘PHP tutorial’ available at http://www.w3schools.com/php/default.asp [accessed 02 February 2012].
Zend Developer Zone (2011) available at http://devzone.zend.com/ [accessed 02 February 2012].