dan forys


Learning PHP - Part 1: introduction

Posted on 2009-10-24 - Comments

So, you’ve got to grips with the basics of HTML and CSS, and now you want to actually make your web page do something? Well, PHP is a great way to do that. In this series of blog posts, I will introduce the PHP language and teach many useful techniques that are used in professional PHP development.

What is PHP?

PHP is a general purpose scripting language. At its most basic level, i t can be used to embed logic into web pages. If HTML is the meaning and CSS is the presentation, PHP is the engine that makes it work. As you will see later in the series, it also has powerful object oriented abilities that help you produce reusable and easily maintainable code. For a detailed history of PHP, see the excellent Wikipedia entry.

What you need to get started

First, you’ll need a web server. You won’t get very far unless you can actually see the web pages you create. Personally, I prefer using Apache – and indeed, this guide assumes you are using Apache.

There are several ways to get Apache up and running, for Windows users there is WAMP or EasyPHP. For Mac users, OS X has Apache built in, or you can use a self-contained environment such as MAMP.

My preferred platform for the web is Linux – as far as I’m concerned, Linux is the OS of the web. Whilst Windows is an excellent general purpose OS, it has some major differences with Linux that can catch out the unwary. It’s dead easy to either get a Linux-based web host, or set up a virtual machine on Windows.

Finally, you need an editor. Some purists prefer to use simple text editors, the best of which give you syntax highlighting to make your life easier. Personally, I use NetBeans. Handily, NetBeans is available for the Mac, Windows and Linux. I prefer NetBeans because it automatically highlights any PHP errors as you type – an invaluable feature. It also offers some useful advanced features that will appear in this blog later.

First steps

By default, Apache will execute PHP embedded into any file with a .php extension. Create a new file called, for example, my-first.php and enter the following:

echo 'My first PHP file!';

The <?php and ?> are the opening and closing tags respectively. By default, PHP uses an XML style syntax to denote where your PHP starts and stops. Anything outside these tags is sent to the browser verbatim. You could have HTML markup outside these tags for example.

Anything within the tags is intercepted by the PHP engine before the page is sent to the browser. In this case, the ‘echo’ causes the content inbetween the single quotes to be sent to the browser. Note the semicolon at the end of the line – PHP uses the semicolon to signify the end of the line. It’s a very common mistake to leave out the semicolon, which is why using a good editor like netbeans will highlight the mistake for you.

To run your first PHP script, you’ll need to put it on your server. This will mean copying it to your document root, or uploading it using FTP. Once the file is in place, you should be able to run it by going to http://localhost/my-first.php if it is on your local machine, or http://whatever-your-host-is.com/my-first.php if you’re hosting it somewhere remotely.

If all went to plan, you should see a blank page with the text ‘My first PHP file!’ on it. It’s not much, but an important first step into the world of the dynamic web.