Before we begin working with PHP, we must first install it. You see, PHP is an application like any other. It needs to be installed on our system before it can process PHP code.
The method of installation varies greatly depending on the operating system that we are using. For that reason, I've provided three different guides for installing PHP. The first section will explain how to install PHP on a Linux distribution, namely Ubuntu due to its popularity. The second section will explain how to install PHP on an Apple Mac OSX system. Finally, the third section will explain how to install PHP on the Windows operating system.
We'll only be installing the console version of PHP. We won't be setting up a web server just yet. We'll get to that in a later title. The console version of PHP is all we need to get started with our learning process.
T> Remember, you only need to read the appropriate section for your computer. Once you have PHP installed, go ahead and skip to the next chapter of the book.
The best way to install PHP on a unix-based Linux distribution is to use a package manager. The package manager available depends greatly upon the distribution of Linux that you have chosen. I've decided to provide instructions for installing PHP on Ubuntu, one of the more popular distributions of Linux.
Ubuntu uses the
apt package manager to install its packages. To install the console version of PHP we need to install the
php5-cli package. Let's do this now. First open a new terminal. You'll need to type the following instruction.
$ sudo apt-get install php5-cli
You don't need to type the dollar sign, that's just the terminal prompt to show you that we're typing it into the console. Once you hit enter, apt will retrieve the PHP application package, and install it for you.
That's it! You're done. well you should be. Let's check, shall we? Simply type...
$ php -v
This command is used to show the current version of PHP installed. You should see something similar to the following.
PHP 5.5.13 (cli) (built: Jun 5 2014 19:13:23) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
Yours won't be exactly the same, after all, we're all different, aren't we? In the example above, the PHP version is
5.5.13. Hopefully your PHP version number should be
5.4.0 or greater.
If your version isn't right then you'll have to consult the documentation for your Linux distribution to find out how to install the appropriate version.
Go ahead and skip to the next chapter, you're done!
On the Macintosh operating system, PHP comes pre-installed. Go ahead, open up the Terminal application and type the following to find the version of PHP you're using.
$ php -v
Don't type the dollar sign, that's the terminal prompt! You should see something similar to the following, but not exactly the same.
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
The PHP version in the example above is
5.4.24. As long as your version of PHP is greater than
5.4 then you're fine, and can move to the next chapter.
If yours isn't, that's okay. We can use a third party package manager for OSX to install a newer version of PHP.
We're going to use a package manager called 'Homebrew' or just 'Brew' for short. To install Homebrew, follow the instructions found on the following site:
I don't want to copy the instructions here, as they often change between different releases. Once you have Homebrew installed, it's time to install a newer version of PHP. I recommend installing version
5.5. You can do this using the following command.
$ brew install php55
Next you need to add the location for this version of PHP to your system PATH variable. Don't worry, just type the following.
You may need to update the version number to match the version of PHP that Homebrew has installed on your system. Now let's have another go at checking the version of PHP.
$ php -v
Hopefully, this time you'll have a version greater than PHP
5.4. Go ahead and skip to the next chapter.
Installing PHP on Windows is a little more difficult, at least for me it is. I've tested the instructions below on my Windows 10 machine, but if you have any difficulty replicating these steps, let me know and I'll find someone who's more Windows-savvy to rewrite this section.
First, head over to:
Here you'll want to download the latest PHP 5.4 and above zip archive. Once the archive has been downloaded, you'll want to extract it to a sensible location. I chose to extract mine here:
You're going to need a command prompt to execute the scripts that we write in this book. So here's a great way of running a command prompt on Windows.
Right click on your desktop, or any folder and choose 'Create Shortcut'. In the target box enter:
Click next, and name your shortcut "PHP".
Finally, you'll want to right click your shortcut and click 'Properties'. On the 'Shortcut' tab, change the 'Start In' field to match the location where you extracted the PHP archive. Click 'OK; when done.
Double click on your PHP shortcut, and you should be greeted with a command prompt. Type...
..and you should be greeted with the PHP version information. Confirm that the version is greater than or equal to PHP 5.4, and then move to the next chapter.
Once again, sorry for the roughness of this subchapter. I've not used Windows as a development machine for a number of years now. If anyone has a better way of running PHP on Windows, kindly email your instructions to receive your 5 minutes of fame within this chapter!
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!