PHP Tutorials

Tutorial 1 - Introducing PHP

PHP stands for PHP Hypertext Preprocessor (yep, the acronym of PHP is recursive - it contains the term PHP within the acronym!). PHP is a server side scripting language. What that basically means is that PHP peforms certain actions 'behind the scenes', and then outputs the results as part of the HTML page that a visitor is viewing. As such, the actual PHP code isn't revealed to the user (like it is with JavaScript, for example).

PHP can have a number of uses from, for example, simply outputting a different welcome message to a visitor depending on the time of the day, to contacting a database, performing a query, and returning the results to the user.


Quick Reference:
A Basic Example of PHP
Uses of PHP

A Basic Example of PHP Top

Whilst this article is only a introduction, it's probably best to give an example of how PHP works. The below script is a bit of PHP that is imbedded into a HTML page. Once a user visits the page, the PHP is processed before anything else. Its output is then sent onto the HTML script, without the user being able to see the PHP code.


<html>
<head>
<title>Example Page</title>
</head>
<body>
<h1>PHP Example</h1>
<?php
// This is a comment. It doesn't affect the script. The line below will 'echo' -
// or output - the phrase "Hello World!" to the HTML script
echo 'Hello World!';

?>

</body>
</html>

As you can see, this code has the PHP embedded in it (the PHP is the coloured portion). This is a great benefit of PHP - you can simply insert some PHP code anywhere within the HTML file; it doesn't need to be written in a seperate file and included in, like in other scripting/programming languages. Keeping on with the above example, if a user was to visit that page, the following HTML would be sent:


<
html>
<head>
<title>Example Page</title>
</head>
<body>
<h1>PHP Example</h1>
Hello World!
</body>
</html>

As you can see, only the HTML code is output. This is why PHP is a"server side" scripting language - it only operates on the server, and the user never gets to see the PHP code.

Obviously there isn't too much point to the above example, because the phrase "Hello World" could simply be written in as part of the HTML. It's merely to illustrate how PHP works and looks. However there are numerous uses for PHP, as detailed below.

Uses of PHP Top

The PHP.net website sums up this question best: "What can PHP do? Anything."! That pretty much is true. Using PHP, you can take a static (HTML only) website, and turn any part of it into a dynamic (changing) website based on any sort of criteria. It's impossible to make a specific list of things that PHP can do, although a few examples can be seen simply by looking around this website. For example, look above at the first code box. Notice that the PHP is colour-coded. Do you think that that was colour-coded by hand? If so, you would unfortunately be wrong; the colour-coding there was actually done automatically by a PHP script!

Another example of PHP at use is on our Contact Us page. Once you fill your details into the form on that box, a PHP script checks that everything is correct, and either displays an error if any part of the data given is incorrect, or it generates an e-mail and sends it off if everything is correct.

The final example I'll give is forums, where users login and post messages for other people to see. Pretty much everything you see there is generated via PHP. When you log in, you stay logged in due to PHP. The various boards and topics come from a database, which PHP communicates with and then outputs. Pretty cool stuff, no?