Skip Navigation

Blog » PHP for Beginners

An introduction to PHP, one of the most popular programming languages on the web.

PHP stands for PHP: Hypertext Preprocessor (yes, it is a recursive acronym, in that PHP is one of the words of the acronym), and is a scripting language that can usually be found running on Apache servers. It is an Open Source language (for more information about Open Source, visit OpenSource.org). It is very powerful, and very easy to learn, which along with the fact it runs on Apache (Open Source) and is itself Open Source makes it one of the most popular scripting languages in use on the web today.

There are several versions of PHP, the current one being PHP 4, though at the time of writing PHP 5 is due for imminent release.

PHP pages usually end with the extension ".php", ".phtml" or ".php3", for example "index.php". Pages that have a recognised extension, on a properly configured server, will be processed by the PHP scripting engine before being returned to the user. Anything within the tags '<?php' and '?>' is code and will be processed by the engine.

Which brings us neatly to our first piece of code. The first thing you will learn to do with PHP is use it to write a simple piece of text to a web page (the famous "Hello World").

<html>
    <body>
        <?php
            echo 'Hello World!';
        ?>
    </body>
</html>

Copy and paste the above to an empty text file (for example in Notepad). Save it as "php101.php", and upload it to a server that supports PHP. When you load the page in a browser, you should see the text "Hello World!". If you do, congratulations! You've finished your first script. If not, it may be that your server does not support PHP - the above should work on any server with PHP support.

What you have done in the above example is write out a normal HTML page, but you have used the PHP scripting engine to write out the text within the page. A good start, but not the most useful of tools so far. Next, we'll learn how to use basic variables.

Variables in PHP are very easy to use. You can spot a variable, because its name will almost always begin with a "$" sign. It is always a good idea to name your variables so that you can see easily what they refer to. For example, a variable containing a first name might be called "$FirstName".

To use these in a script is reasonably simple:

<html>
    <body>
        <?php
            $HelloWorld = 'Hello World, Again!';
            echo $HelloWorld;
        ?>
    </body>
</html>

Note that when you use the variable with the "echo" section, you should not put quotation marks around it. If you do, the script will just write out "$HelloWorld" as text, rather than writing the value you gave it.

The above is a simple process - a variable is created and assigned the value "Hello World, Again!" and written out to the web page. If you copy and paste the above as before, and upload the new script and open it in a browser, you should now see the text "Hello World, Again!" on the page. If you do, congratulations, you have just finished your second PHP script.

Still, though, not the most impressive piece of interaction on the web. Next though, we're going to write a more complex script. This time, we're going to create a variable, give it a value of the current hour of the day, and display a piece of text based upon whether it is morning or afternoon.

<html>
    <body>
        <?php
            $HourOfDay = date("G");
            if ($HourOfDay < 12) {
                echo 'Good Morning World!<br><br>The hour of the day is ' . $HourOfDay;
            } else {
                echo 'Good Afternoon World!<br><br>The hour of the day is ' . $HourOfDay;
            }
        ?>
    </body>
</html>

First, we grab the current time using the "date()" function. The argument, "G" (an argument is a value you pass a function) tells the date function to give us the hour of the day, with leading zeroes. At this stage, $HourOfDay will contain a number representing the hour of the current day.

Next, we check the value. If the value is less than 12, we know the current time is morning, and write out an appropriate greeting, followed by the hour of the day (you use a period to write more than one thing to a page at once). Otherwise (else) it is afternoon, and we write out a different greeting.

Now, to use this, you simply need to upload this script, as before, to your web server, and open it in a web browser. Your server may be on a different time zone to you, but you can see what hour of the day it thinks it is underneath the greeting. What you should now see is a greeting of either "Good Morning World!" or "Good Afternoon World!" followed by a blank line, then the hour of the day.

And voila - your first piece of interactive web programming. Congratulations, you've taken your first steps in PHP!


comments powered by Disqus