
Welcome to PHP! “PHP” officially stands for “PHP: Hypertext Preprocessor,” but millions of people still know it by its original name, Personal Home Page, and that’s what it’s all aboutcreating your own interactive web pages in the easiest possible way. No longer will web pages have to be static, unchanging things. Now you can send the user new web pages tailored to what he or she wants to see in real time. You can handle button clicks, checkbox selections, and radio buttons and can even draw graphics interactively and store data in a database. It’s all up to you the lid is off the box.
This is where you make your web pages come alive.
We’ll be using PHP version 5, whose web site is www.php.net, in this book. PHP is specially designed to make creating your own web pages a snap. In this book, we’re going to work from the server side by installing PHP scripts on your web server. Users will be able to open those scripts in a web browser, seeing everything they’d expect from a fully fledged page: text fields, tables full of data created on-the-fly from databases, and fluid graphicseverything you might see on the most professional interactive web page is now within your grasp.
With static web pages written in simple HTML, a web server will just pass the HTML in a web page back to the browser, and the user can see pictures and text, but that’s about it. When you write sever-side scripts in PHP, on the other hand, you actually tell the server what you want to doread what the user entered in a text field, see which checkboxes were clicked, and so on. Then you can decide what to do next, and you can create the web page to send back on-the-fly. That’s the name of the game herebeing able to respond dynamically.
PHP has become a huge successmore than 15 million web pages use it now. In the following pages, we’ll see how to make the web server do what we want with PHP.
The first step to creating your own interactive web pages is to get access to a web server that runs PHP. In fact, your Internet Service Provider (ISP) probably supports PHP already. You can find out by asking your ISP’s support staff, or you can test for PHP yourself in one of two ways.
First, if you can open a command prompt by connecting to your web server using a Telnet, SSH, or SSH2 application (don’t worry if you don’t know what these applications areyou won’t need them in this book), you can try typing php -v at the command line (we’ll use a % sign for a generic command prompt in this book). If you have PHP installed, you’ll see something like this:
%php -v
PHP 5.0.0 (cli) (built: Jul 13 2004 21:39:58)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.0, Copyright (c) 1998-2004 Zend Technologies
The other way to determine whether you have PHP installed is by trying out an actual PHP script. To do that, take a look at the section “Creating Your First PHP Script” in this chapter. If that script works, you’ve got PHP installed, and you’re all set.
If you want a list of ISPs that already run PHP, take a look at www.php.net/links.php#hosts.
Installing PHP Locally
It’s a good idea to install PHP on your own computer so that you can test your PHP scripts as you develop them. This way, you won’t have to take the time to upload your PHP scripts to your ISP, check them by downloading them in your browser, make changes, and then start the whole cycle again. If you develop your PHP scripts locally, you can get things running a lot faster, but you’ll need to install PHP on your own machine.
Some operating systems, such as Linux and many versions of Unix, now come with PHP installed by default. In others, such as Windows and Mac OSX, you’ll have to download and install PHP yourself. The first step is to check if you already have PHP available locallytry the php -v command at the command prompt (for example, in Windows, open a DOS window and type php -v). If it works, you’re all set.
If you don’t have PHP already installed, you can install it yourself. Prebuilt “binary” versions are available for download and immediate installation for a number of operating systems: Windows, Mac OSX, Novell NetWare, OS/2, RISC OS, SGI IRIX 6.5.x, and AS/400. You can find the binary installation package for Windows at http://www.php.net/downloads.php, along with links to the binaries for the other operating systems mentioned.

Binaries are no longer distributed for Linux and Unix because PHP is usually pre-installed. If you want, you can build your own PHP installation from source code. Go to http://www.php.net/downloads.php to get the source code for PHP.
You can find the installation instructions for PHP in the PHP documentation, which is online at http://www.php.net/docs.php (you can download the complete PHP documentation from http://www.php.net/download-docs.php). You can also find installation instructions in an installation file (named, for example, install.txt) when you uncompress the PHP download. Because the instructions change every time PHP changes, and because there are so many possible variations of operating systems and web servers, you should read the current installation instructions and use them. Listing all the instructions here would take 20 pages, and they’d be obsolete by the time you read them.
Briefly, here’s how things might work in Windows XP (all this can be found in full detail in the downloadable installation instructions). You first need a web server, such as the Apache web server or Microsoft’s Internet Information Server (IIS). You can get a Windows installer file for Apache (apache_2.0.52-win32-x86-no_ssl.msi) at http://httpd.apache.org/ download.cgi; when you download and double-click this file, it installs Apache. Or you can install IIS in Windows XP by using the Add/Remove Programs icon in the Control Panel, clicking the Add/Remove Windows Components button, and then selecting IIS.
PHP can be installed in two different ways for Windows, and you can download what you need for both methods from http://www.php.net/downloads.php. There’s a Windows installer executable file, php-5.0.x-installer.exe, with basic PHP support (which includes standard PHP but no external extensions). This installer will automatically configure servers such as IIS, PWS, and Xitami, and it has instructions for manual configuration of other servers such as Apache.
The other installation technique uses a .zip file, php-5.0.x-Win32.zip, which contains the full PHP installation and allows for external extensions (this option is better if you want to do everything covered in this book). Download and unzip this file; you’ll find all the installation instructions included. Depending on your operating system, you typically copy php.exe to the directory from which you intend to run it, for example.
The next step is to connect your PHP installation to your web server. If you’ve unzipped the PHP .zip file in Windows XP and you’re using Apache, you should edit the Apache http.conf file, following the directions in the installation instructions. If you’re using IIS, you configure IIS with its management console (select Start > Settings > Control Panel > Administrative Tools > Internet Services Manager in Windows XP), also following the directions in the installation instructions. For the full details, see the instructions, which take you through everything step by step.
Setting Up Your Development Environment
To create PHP pages, you’ll need a text editor of some kind to create PHP files, which are a mix of HTML and PHP. All kinds of text editors are available on numerous operating systems that can serve the purpose, such as vi, emacs, pico, Macintosh’s BBEdit or SimpleText, and Windows Notepad or WordPad. By default, PHP files are given the extension .php (as in myBigTimeWebPage.php).
The text you enter into our pages is just plain text, a mix of HTML and PHP. You can see an example in Figure 1-1; to create this PHP-enabled page, you just enter the text as it appears in the figure and save it as a file with the extension .php. As you can see, this example is mostly HTML; the PHP part is the script that appears between the <?php and the ?>. When you navigate to this document in your browser, the PHP-enabled server will read the document, find the PHP part between the <?php and the ?>, and execute it automatically. In this case, our PHP is just the single line phpinfo();, which will display an HTML table full of information about the PHP installation on the server, as we’re about to see in “Creating Your First PHP Script.”

Figure 1-1. Creating a PHP page with HTML in it.

Windows WordPad has the annoying habit of appending the extension .txt to a filename if it doesn’t understand the extension you’ve given the file. If you try to save a text document with the extension .php, WordPad will give it the extension .php.txt, which is very annoying. To avoid that, surround the name of the file with quotation marks when you save it, as in “logbook.php”. Also note that by default, WordPad saves files in rich text format (.rtf) or as Microsoft Word (.doc) files, depending on your version of Windows. To make sure you save your XML documents in plain text format, select the Text Document option in the Save as type drop-down list box in the Save As dialog.
In fact, you can even use a word processor such as Microsoft Word to create PHP documents if you save your documents in plain text format, not in some other format such as .doc (for example, in Microsoft Word, you’d select the Text Only option from the Save as type drop-down list box in the Save As dialog). It’s much better to stick with a plain text editor than a word processor, however, because unless you’re careful, word processors will add all kinds of features and decorations that will get in the way.
You can also use a PHP integrated development environment (IDE) to create your PHP pages. IDEs give you all kinds of tools that simple text editors don’t, such as checking what you’ve written automatically to make sure it’s valid PHP, automatic syntax highlighting (where items such as PHP keywords appear in various colors, making it easy to pick out what’s going on at a glance), and automatic deployment, where the IDE can upload your PHP pages to your ISP when you click a button or select a menu item.
Here’s a starter list of IDEs available online that can handle PHP. Note, however, that most of them cost money, and although they have some nice features, we won’t rely on those features in this book:
- Komodo (www.activestate.com/Products/Komodo) runs on Linux and Windows.
- Maguma (www.maguma.com) runs on Windows only.
- PHPEdit (www.phpedit.com/products/PHPEdit) is free, but runs on Windows only.
- Zend Studio (www.zend.com/store/products/zend-studio.php) runs on Windows and Linux. This one is created by the same people who created the Zend software “engine” that actually runs at the core of PHP itself.
If you’re working with an ISP, you’ll also need some way of transporting your PHP pages to the ISP, as you would with standard web pages. You can use a File Transfer Protocol (FTP) program or a web interface if one is provided by your ISP. If you haven’t uploaded web pages before, ask your ISP support team what they recommend because they will often point you to an FTP program that they support or a web interface they’ve set up to allow people to upload web pages. You upload and run PHP pages just as you would standard HTML pages, as long as your server supports PHP.

