PHP: An Introduction
Overview

PHP is a "backronym" (or "recursive acronym") for PHP HyperText Preprocessor. Although PHP can actually be used for any number of purposes outside of the realm of web design, it is easily most well known for its use as a web scripting language. The "hypertext preprocessing" that it does is server-side programming (communicating with a database, manipulating files, and performing calculations on the central server hosting the site) before outputting a result the browser in the form of HyperText Markup Language (HTML). PHP is coded within a file (typically with the .php extension instead of .html) between <?php and ?> tags that are pre-processed for output. The output that is generated between each set of tags appears in the HTML in the same location as the original opening and closing PHP tags in the page.

Getting Started
Installing an Apache/PHP Environment

PHP is often run within what is known as a LAMP environment: Linux, Apache, MySQL and PHP. The operating system (Linux) is easily switched up with Windows or Mac, and the other aspects are fairly interchangeable as well. Instead of the MySQL database, one might choose to use something like Oracle or MSSQL, and instead of an Apache web server, one might run PHP on a Windows IIS setup. However, no matter what your operating system, the _AMP (Apache/MySQL/PHP) setup is quite popular, and there are several easily downloadable automatically-configured environments available on both Mac and Windows platforms. Below are links to the sites for two of the most popular, and I strongly recommend starting out with one of these environments first. If you already know how to install the appropriate _AMP setup on your machine, or already have, then of course you can disregard this part.

  • WAMP (for Windows users)
  • MAMP (for Mac users)
Outputting to a Browser

To test our setup, let's jump right into outputting some PHP to a page. We'll use a typical "hello world" example. The only function we'll use for now is "print", which (unlike many other functions in PHP) can be called without using parentheses and simply prints whatever string that follows (within quotes).

To create your first page, open up a folder to your root directory. In MAMP, this directory will probably be under /Applications/MAMP/htdocs/ (or perhaps a different location, depending on where your MAMP files were installed). In WAMP, it will be under a directory with a similar structure, and ultimately within an 'htdocs' or 'www' folder. Create a new file there called test.php, and enter the following code:

Note the semicolon at the end of the line. If you have prior programming experience, this is probably nothing new, but just in case, I'll point out that this semicolon is standard indicator that you're done issuing a statement or command. In some languages, it is optional. In PHP, it is mandatory.

You should now be able to navigate to your localhost URL to view your web page as it appears after it has it been processed using the PHP engine. If you've followed the setup instructions for MAMP or WAMP above, your address is probably http://localhost/test.php, or perhaps there is a port number after it (like http://localhost:8888/test.php, or something like that).

Below is the output that is produced. Note the position of the string that we printed: directly between the "Hello" and the "!", because that's where the code that generated it was located in our PHP file:

If this is what you see, then you're ready to dive into more PHP programming! If not, then take a closer look at the installation instructions for your selected operating system and server environment. Ultimately, to proceed with this tutorial, you need to have a server set up to process and serve up PHP pages, and be aware of where to create the files that will be used on your site.


Recommended next: PHP Programming Basics