PHP has become one of the most popular languages for programming on the web. Many popular software packages such as WordPress, Gallery, and ZenCart are built using PHP. If you are planning on using any of these, or doing any sort of web programming, it is useful to at least be familiar with PHP.
This is the first in a series of articles on getting started with PHP. I will first explain the basics of what you need to write and run programs in PHP, and then I will walk you through writing your first PHP program.
The PHP Runtime
The first thing you need to start writing PHP programs is the PHP runtime. The PHP runtime is what will interpret your script when a page is requested, and output the results as HTML to the web browser. Most hosting companies already have PHP installed, so you can probably skip this step.
However, if you are hosting the site on your own web server, you will need to download the PHP runtime here. Depending on the type of web server you have (Apache, IIS, OS X Server), the installation instructions can vary pretty widely, so I will just direct you to the official installation instructions.
The Basic Elements of a PHP Script
Now that you’re ready to start writing your first PHP script, let’s go over the basic elements that are involved:
- There are a number of specialized PHP editors available, including Komodo, PHPDesigner, and Zend Studio, although I prefer to use a simple text editor like TextPad.
- PHP pages should have a .php file extension.
- Inside of your page, PHP script blocks must be enclosed in <?php ?> tags.
- Regular HTML can be embedded inside of your PHP page, but must be located outside of any PHP script blocks.
Writing Your First Program
Now that we are familiar with the basic elements of a PHP script, we are ready to write our first PHP program. The Hello World! program has long been the standard first program when learning a new programming language, as it performs the most basic of tasks (it displays “Hello World!” on the screen). Let’s take a look at what the Hello World! program looks like in PHP:
<?php
echo "Hello World!";
?>
Pretty simple, huh? Let’s take a closer look at exactly what’s going on here:
- The echo function simply displays to the screen whatever follows it. There is also a print function that behaves similarly, but echo is actually a little bit faster, so I usually like to use it instead.
- Since “Hello World!” is a literal string of characters, it must be enclosed in quotes. If I wanted to display the number 1, I could have done this instead: echo 1;
- The line ends with a semicolon. Every line in a PHP script must end in a semicolon. This is what tells the PHP runtime that the current command is complete. Otherwise, the PHP runtime will try to combine it with the command on the next line, and you will likely get a runtime error.
For Next Time
After reading this article, you should be able to write a basic PHP program that displays “Hello World!” in the browser. The next article in this series will focus on one of the major building blocks needed for writing more interesting programs: variables.
You can already read Getting Started with PHP: Part 2 – Variables