Hand Crafted Software

 

A Brief Introduction to PHP

PHP is an interpreted scripting language that is embedded within an HTML web page in order to add dynamic processing to that page.

PHP is supported by a wide range of commercial and open-source web servers, including RedHat Linux, and can also be installed as an Apache module. Its widespread availability and its relative simplicity mean that it is an excellent way to introduce dynamic features into your web pages. As it is an open, non-proprietary standard, PHP developers are not restricted by the limitations imposed by some commercial suppliers of server-side scripting software, neither do they have to purchase expensive licenses in order to use it.

You may already be familiar with 'client-side' scripting languages such as JavaScript. If you include JavaScript in your page, then the JavaScript code is downloaded to the client's browser and executed there. PHP is different in that it is strictly a 'server-side' scripting language - this means that the PHP is always processed by the web server before the requested page is served to the browser. The PHP tags in the page are replaced by generated HTML strings and the client's browser then displays the HTML without any knowledge of the underlying PHP code at all.

The syntax of the language is similar to C, so anyone who is familiar with the C programming language, (or Perl or Java, for that matter) should be able to master PHP scripting quickly and without too much difficulty. Object Orientated Programming extensions have been introduced with the latest release of PHP which allow you to use objects within a PHP script.

PHP can be used to do anything that any CGI program can do, such as:

One of PHP's biggest strengths is its ability to interface with databases. PHP supports a wide range of databases, including proprietary (such as Sybase and Oracle) and open-source (such as MySQL and PostgreSQL). The complete list is growing all the time. PHP can also communicate with other processes using a variety of standard protocols.

The main disadvantage of PHP is that it is an interpreted language and therefore there is inevitably a overhead in processing a page of PHP script. However, no PHP code is ever downloaded to the client's browser so there is never any question of a user 'stealing' your PHP code and adapting it for his own purposes.

PHP is a very powerful program that can access files, execute commands and open network connections on the server. These features would make anything being run on the server insecure by default. Because PHP is specifically designed to operate on web servers then it is intrinsically more secure that 'general purpose' languages such as C or Perl. PHP has a number of different configuration options which give the web server manager the ability to set precisely the level of security that is needed for the situation.

<html>

<head>

<title>Example</title>

</head>

<body>

<?php echo "Hello, World!"; ? >

</body>

</html>

The PHP script is between the tags <?php and ?>, which are used to instruct the server to 'enter PHP mode' and 'exit PHP mode' respectively. There can be any number of pairs of <?php and ?> tags within an HTML file, although if the tags are not correctly paired then the page will almost certainly fail to be displayed.

All the PHP code does is in the above example is to echo a text string. Because this text string is output within the <body> section of the web page, the text will be displayed by the browser.

PHP Variables

In PHP a variable is prefixed by a $ sign. A variable does not need formal declaration, nor do variables need to have declared types. A variable can even change its meaning and type during its lifetime.

The following example uses a string variable called $greeting, which is assigned with the value "Hello". If you are familiar with C or Perl then it should be fairly easy to work out what this example does.

<?php

$greeting="Hello";

echo $greeting ", World!";

?>

HTML Forms and PHP

When an HTML form is submitted to a PHP script, the variables from that form are automatically be made available to the script by PHP. Therefore, if a user enters data into a named input field, a variable with that name becomes available to the PHP script, already initialized with the value entered by the user.

The following example illustrates this:

<FORM METHOD="GET" ACTION="submit.php">

What's your name? <INPUT NAME="name" SIZE=3>

</FORM>

This fragment of HTML merely creates a form with one data entry field called "name". When the user enters some text into the name field and the form is submitted, the server calls the script submit.php with the variable $name already initialized. The following snippet of PHP code shows how this variable can be used.

<?php

echo "Hello, $name!";

?>

PHP Functions

As it is a structured programming language, PHP allows you to organize your script by the use of functions. A function is defined using the keyword 'function', followed by the name of the function and an argument list in parenthesis. The body of the function is enclosed in curly brackets, exactly like a C function. For example:

function example($variable1, $variable2) {

echo "Example function\n" ;

return $retval ;

}

Other than the 'function' keyword, this syntax will be familiar to anyone who is familiar with C functions.

There is also a library of built-in functions available. These give you the ability to perform tasks such as:

There are also freely downloadable libraries available that also allow you to carry out the following:

Working With Cookies

You can set cookies in the browser from PHP using the setcookie() function. setcookie() adds headers to the HTTP response. As headers must be inserted before the body of the page, you will need to finish all of your setcookie() calls before any body output (usually HTML) is printed.

<?php

if (!$name) {

echo "What is your name? ";

echo "<FORM ACTION=\"$PHP_SELF\" METHOD=\"GET\">\n";

echo "<INPUT NAME=\"name\" SIZE=20>\n";

echo "</FORM>";

exit;

} setcookie("name", $name);

?>

 

PHP and Databases

Probably PHP's most powerful feature is its ability to interface with a wide range of databases. PHP includes a huge set of in-built functions to provide an interface between the PHP script and the database software. The PHP manual (available from the PHP website) provides a full list of all the functions that are available for each supported database.

In general, functions exist for making a connection to a database, updating and querying the database (using SQL) and retrieving results from the database.

And Finally: What Does 'PHP' stand for?

When the prototype of PHP was originally devised in 1994 by Rasmus Lerdorf, it was given the working title of Personal Home Page Tools. Rasmus later combined Personal Home Page Tools with another application he had written (Form Interpreter, or FI) to create PHP/FI. This system grew at an amazing pace and although the name PHP stuck, the original meaning became less and less relevant. Eventually PHP was rewritten and repackaged with the name 'PHP: Hypertext Preprocessor'. Most people ignore that, however, and simply refer to it as PHP.

The PHP homepage - from which source code, detailed documentation and binary distributions can be downloaded for a number of platforms (including Windows) - can be found at http://www.php.net.

 

Back Home

© 2003 Richard Young. All rights reserved.