PHP - The PHP Hypertext Pre-processor

What you need to know

First, you should be aware that this is not a tutorial for complete beginners. There are already a large number of resources that can cover an introduction to programming far better than I could ever hope to. For those who are new to server-side programming, I would suggest a search through the code section of, and the excellent Newbie Network.

That said, I will not assume a great deal of specific knowledge of PHP. These tutorials will be written for those of you who have some limited programming knowledge, and are able to rapidly pick up the specific details of a new syntax whilst recognising the underlying principles. If you have some prior experience with a web-centric language (ASP, JavaScript, Java, PERL, etc.) then you'll be fine.

For those that do not have access to a PHP server, I would suggest heading over to our very own which provides free web hosting, including PHP, MySQL, and an e-mail account for the wonderful purpose of helping developers like us in a friendly environment. All the code that will form these tutorials has been developed as part of my learning process, and much if it now forms my own site. Needless to say, everything I discuss should work for any account.

Getting to it: your first PHP script

No "hello world" applications here. We're going to dive right in with a simple form acknowledgement script that (for now) does nothing more than output the contents of a simple HTTP POST form to an HTML page. First the code, then a brief explanation.

Please ignore the fact that some vital HTML is missing. Consider this to be pseudo-code for exemplary purposes only. You can find links to all of the complete code at the bottom of the page.

The form input, your_comments.html



<title>Please send us your comments</title>



<p>Enter your details in the boxes below, then hit the submit button.</p>

<form action="ack_comments.php" method="post">

your name : <input type="text" size="20" name="your_name"><br>

your email : <input type="text" size="20" name="your_email"><br>

your comments : <textarea cols="30" rows="10" wrap="virtual" name="your_comments">Some comments here...</textarea><br>

<input type="submit" name="submit" value="submit">




Right, this is your most basic HTML form. Hopefully, it shouldn't require any comment or explanation. The only thing to note that is we are POSTing to the script "ack_comments.php".

The form reponse, ack_comments.php


$your_name = $HTTP_POST_VARS['your_name']; // from the array created by the HTTP Request.

$your_email = $HTTP_POST_VARS['your_email'];

$your_comments = $HTTP_POST_VARS['your_comments'];

$error_msg = false; // created as a boolean type, so useful as a flag.

if ($your_email == NULL){

$error_msg = "You did not enter an email address!";

// no email address, so we set the flag true (any non-zero value)





<title>Thanks for your comments</title>



<p>You entered the following information:</p>

<p>your name : <?= $your_name ?><br>

<? if (!$error_msg) {

?>your email : <?= $your_email ?>


} else {

?><?= $error_msg ?>




your comments : <?= $your_comments ?></p>



Let's break this down, step-by-step.

<? and ?> are our script delimiters. PHP will process anything inside these, and treat anything outside them as being a literal string. Basically, when all is done, the results of your script inside the delimiters is concatenated with anything outside the delimiters, and returned as the HTTP Response.

$your_name = $HTTP_POST_VARS['your_name'];

Here we are delcaring a variable $your_name, and assigning a value to it. The value is held in an associative array, in this case a built-in array created by the HTTP Request, called $HTTP_POST_VARS. In common with many languages, you can access the elements of an array either by specifying a zero-relative index (eg: $someArray[2] to get the third element) or, if you have an associative array, by specifying a key (eg: $someAssocArray['colour']). Each element of an array can hold any value, including other arrays, and objects.

Next we have set an error flag, $error_msg, to be false. We have declared $error_msg as a standard boolean type (either true or false). PHP treats false and 0 (zero) to be equivalent when evaluating a boolean expression, and any other value (including strings, objects, and negative integers) are treated as true.

We now make use of this behaviour by checking to see if the variable $your_email holds a value. Later, we will add checking for a valid address, but for now we just check to see if the variable is NULL (ie: not set). If it is not, we assign a string to $error_msg, which we will display later. Equality is tested using the == operator, and the expression must be enclosed within parentheses. Our if-statement uses braces ({}) because the conditional spans more than 1 line. We could have written:

if ($is_this_true)

do this;

which would be identical, but can cause confusion for anyone that's reading your code and expecting the braces.

With our variables all assigned, we can get down to the business of displaying the results. We close off the PHP script tag (?>), and start writing out the HTML page. You'll notice that when we write out the variable $your_name, we use a shortened syntax within the HTML : <?= $your_name ?>. This is a shortcut for the language construct echo(value). ASP coders will recognise this as being a handy way of substituting script variables into your HTML markup without the need for lengthy blocks of script.

The last part of interest in this script lies with the final if-statement. We are now testing the value of $error_msg, which needs to return either true or false. This makes sense when we consider that PHP treats non-zero values as true, including strings. So, if an error message has been assigned to $error_msg, it will be displayed.

Your Next Step

If you have not already done so, grab the code listings above, put them into appropriate files, and try them out.

Finally, please, please use the comment facility below to provide feedback on these articles. This is the first tutorial that I have written, and I can only make them better if you tell me how. You can also email me directly for further discussion. If you have questions about any aspect of PHP, you can pose it to's mailing list where there is a high chance that I will answer it.