PHP 5.x Installation on Windows


Websites are generally grouped into two categories, static and dynamic. As the terms suggest, static sites are those that do not change based upon the actions of the user (the person visiting the site, whether logged in or not), while dynamic sites can change their content, appearance, and data stored (usually on the backend, i.e., on the remote server hosting the site).

There are a number of web technologies that one can use for building a dynamic site, and many of these are referred to as "web scripting" languages. The one most commonly used is PHP.

PHP logo
Figure 1. PHP logo

Why choose PHP over the other options? It is open-source software, and thus you or any other programmer can examine the source code to look for problems; and countless programmers do just that, improving the product. It is cross-platform, which means it can work on a range of operating systems, including Linux, Windows, Unix, and Mac OS X — that first one being the most popular choice for hosting websites. In fact, PHP is part of most Linux distributions, as well as Mac OS X. PHP is an interpreted scripting language, so you do not have to devote time and effort to recompiling your source code each time you change it.

PHP can be embedded directly in HTML pages with the least amount of effort. Because PHP code is inserted within tags, you can readily jump between HTML and PHP, instead of having to rely on large amounts of code to generate and output HTML. However, this should only be done in design templates, otherwise the code can quickly become unmanageable, and data processing code can become painfully intertwined with presentation code.

Because PHP is executed on the server — which you or your client control — visitors to the website cannot view the PHP code, and thus do not know what is going on behind the scenes and what data stores are being accessed — unless you mistakenly allow warnings and error messages to be displayed to the end-user, which would greatly increase the risks of your site falling prey to attack.

PHP is performant enough to power major websites, and works well with a variety of relational database management systems, including MySQL. It also includes a file-based data store, SQLite, which may be adequate for more modest data management needs, and has lower resources and administration overhead.

From the individual's perspective, PHP has a simple learning curve, unlike many other web scripting languages, as well as Java. Also, PHP has built-in command-line capabilities, making it ideal for writing scripts, as an alternative to the venerable DOS shell (cmd.exe), the newer PowerShell, Perl, and any Linux shell languages ported to Windows (such as Bash).

Despite the increasing criticism of PHP by web developers, it continues to grow in popularity, now powering 244+ million websites — almost a quarter billion. Yours could be one of them.

Clearing Out the Deadwood

Before you install the latest version of PHP on your computer, it is wise to first see if it is already installed and visible to the partition on which you would like to do PHP work. Search for old versions of PHP by looking in the default software installation directories. For Windows 7 (the version used in this tutorial), they are %ProgramFiles% (usually C:\Program Files) and %ProgramFiles(x86)% (usually C:\Program Files (x86)). The instructions presented here might apply equally well to Windows Vista and Windows 8. Alternatively, you could use the native Windows file search functionality or a third-party search tool, to scour your PC's hard drive for files named php.exe.

If you do find one or more instances of PHP, you can determine the version number of any one of them by opening a Windows terminal ("All Programs" > "Accessories" > "Command Prompt"), going to the PHP installation directory (containing the executable file php.exe), and entering the command php -v.

If PHP is already installed, then it may be recent enough that there is no need to try to replace it with an even more recent version. If you wish to replace it in either case, then it is advisable to uninstall it fully, to avoid the risk of leftover out-of-date files conflicting with the new version of PHP you are installing, or even conflicting with PHP-dependent applications that you may install later. But before nuking it from your system, save a copy of any configuration changes — typically all found in the file php.ini, which may be located in the PHP installation directory mentioned earlier, or it may be in %WinDir%, because many tutorials recommend that directory as the destination, even though it is bad practice to mix application files in Windows directories, especially if it can be avoided.

If you choose to uninstall an old version of PHP using the typical Windows uninstall process, it should leave behind any customized files, including php.ini. But there are no guarantees, so it is best to make a copy of it before starting the uninstall process, and save the file someplace where you can reference it later in case you need to see what customizations you made in the past.

Installing PHP

You can now begin the process of installing PHP on your machine. Generally it is used in conjunction with the Apache web server. If you do plan on using Apache, and you plan on installing PHP using a Microsoft Installer (MSI) file, then install Apache first, because PHP will be installed into Apache. This is not to imply that Apache must be installed prior to any use of PHP; the latter can be used solely for its command-line capabilities. But typically it is used for dynamically generating web pages locally, in which case you will need a web server. See my earlier article on how to install Apache on Windows.

If you are using 32-bit Apache, then you must choose 32-bit PHP. The PHP download page makes available archive files appropriate for installing PHP on Linux, Mac OS, and every other non-Windows operating system. For the 32-bit Windows installation packages, go to the Windows binaries page.

Windows PHP download page
Figure 2. Windows PHP download page

In the sidebar, under "Which version do I choose?", there is a (somewhat cryptic) warning to not use the VC9 version with binaries. Fortunately, as a Windows user, you should have instead installed Apache using the binaries from Apache Lounge.

On the Windows PHP page, find the section for the most recent stable version (5.4.11, as of this writing), download the installation file for the "VC9 x86 Thread Safe" package (named, and save it somewhere on your computer where you can find it later. Some people prefer using MSI (Microsoft Installer) files instead of Zip archive files, especially as the former use a GUI wizard interface to step one through the process. For PHP 5.4, no such MSI file is currently available, so you must use the Zip file, in order to obtain the latest version of PHP.

If you are using 64-bit Apache, then you must choose 64-bit PHP, as we will do in this article. One source is Anindya's PHP compilations.

Regardless of 32-bit or 64-bit, unzip the installation file, using any file compression application or the one built into Windows. In whatever directory you prefer to install applications, create a new subdirectory for PHP — in this example, C:\_a\PHP. Copy all of the Zip file's contents into the new PHP directory. To verify that all of the files and directories made it into the target directory, in Windows Explorer, select all of them ("Edit" > "Select all", or Ctrl + A), open the context menu on the selected files (right-click the mouse, or hit the Menu key), and choose the Properties menu item. The resultant Properties dialog box should report 71 files and 3 folders.

Verifying PHP

To confirm that PHP works, open a Windows terminal, go to the PHP installation directory, and enter the command php -v, which should emit the version information.

PHP version
Figure 3. PHP version

A slightly more complex test is to create a simple PHP script using whatever text editor you prefer. Windows Notepad is always available, but is far less capable than any programmer's editor. You can even use a word processor, such as Microsoft Word, but you must save the file with the type of "Text Only". Populate the file with some basic PHP code that you know will work.

echo "\n  PHP works!\n";

Save the file with any name, such as PHP_test.php, or rename it if your text editor saved it with a file extension other than .php. Run the script on the command line: php PHP_test.php

PHP test script output
Figure 4. PHP test script output

At this point, PHP is functional enough to be used for executing scripts on the command line. Adding the PHP installation directory path to the Windows Path variable, would allow you to run PHP anywhere without having to prefix the command with the directory path each time.

Path modified
Figure 5. Path modified

In a subsequent article, we will see how to configure PHP, as well as Apache and MySQL, so they work together for displaying locally-hosted websites.

Copyright © 2013 Michael J. Ross. All rights reserved.