Sunday, October 28, 2007

Chapter 1: Introducing PHP


Overview

PHP, which stands for PHP: Hypertext Processor, is a widely used, general-purpose scripting language that is especially suited for Web development and can be embedded into Hypertext Markup Language (HTML). PHP is rapidly spreading through the Internet and is soon to be the leading development language on the Internet. It gives you the ability to interact with users on your website similarly to what you could do on a website using Perl (CGI) and Active Server Pages (ASP).

Because PHP is an open-source project (which means that the source code is available to developers to enhance and expand its functionality and features), the possibilities are limitless. Virtually anyone can contribute to the expansion of the project, unlike commercial products with source code that is updated only by the people who own it. With PHP, you can create membership systems, process credit card transactions, view system statistics, store data in databases, and much, much more. Throughout this book, I cover what you need to know to build your own dynamic website with PHP.

In this chapter, you'll learn all about the history of PHP and how to start using it.


Getting a History Lesson in PHP


In 1994, Rasmus Lerdorf set out on a project called Personal Homepage Tools for tracking access to his online resume. He wrote Personal Homepage Tools in C with a small part of it utilizing Perl. As Personal Homepage Tools grew in popularity and more functionality was needed of it, Rasmus rewrote Personal Homepage Tools utilizing a much larger C implementation that could communicate to databases, which in turn allowed a more dynamic capability. Eventually, Rasmus released a version called Personal Home Page Forms Interpreter (PHP/FI).

Eventually, Rasmus decided to release the source code of PHP/FI so everybody could use it, expand upon it, and fix bugs in it. Even though the project was in the early stages-much more limited than Perl and somewhat inconsistent-PHP/FI was growing in popularity quite rapidly. By 1997, PHP/FI 2.0, the second version, was known to have several thousand users around the world. Some 50,000 domains were reported to have PHP/FI installed, which accounted for one percent of the domains on the Internet. Even though there were several contributions to the project, Rasmus continued to carry the weight and led PHP to the next phase. Shortly after he officially released PHP/FI 2.0 in November of 1997, the initial stages of the popular PHP 3.0 were underway.

PHP 3.0 was the first version of the project that resembles the PHP you see on the Internet today. Andi Gutmans and Zeev Suraski decided to rewrite the PHP/FI parser after they found it did not have enough functionality and power for their requirements. Andi, Rasmus, and Zeev teamed up to continue the project with a much stronger work force than previous versions.

PHP 3.0 was a significant phase in the evolution of this project. It was released under the new name of PHP, which is a recursive acronym for PHP: Hypertext Processor. The new name was derived from a desire to remove the implications of limited personal use from the old name, Personal Home Page Tools. Other significances of this release were the introduction of object-oriented programming (OOP) and a much more powerful and consistent language syntax.

PHP 3.0 was officially released in June 1998 after nine months of public beta testing. By the winter of 1998, PHP was installed on approximately 10 percent of the web servers on the Internet.

The next major release of PHP was version 4.0. In May 2000, the development team officially released 4.0 and introduced more significant changes to the core of PHP. For example, the Zend engine, a complete rewrite of the version 3 core, was implemented as the new PHP engine. Zend (the name composed of the developers' first names, Zeev and Andi) and PHP 4.0 implemented support for many more web servers, HTTP sessions, output buffering, enhanced security, and a wide range of new language constructs.

Currently, PHP is being used by hundreds of thousands of developers and is estimated to be installed on several million web servers, which accounts for more than 30 percent of the web servers on the Internet. At the time of writing this book, PHP has just entered the 4.3.x version.

If you are interested in more accurate and up-to-date statistics for how many domains are using PHP, you can visit these links:

PHP Usage Report www.php.net/usage.php

SecuritySpace's Web Survey www.securityspace.com/s_survey


Programming Community Index www.tiobe.com/tpci.htm


Understanding the Requirements for PHP


Depending on the intentions and design of your scripts, PHP can run on virtually any hardware because of its compact design and system resource requirements. If you are installing on a Unix-based system, then basic Unix skills, including knowledge of make, to build installation binaries and a web server are required. If you are installing on Windows-based systems, administrator privileges and a web server are required.

Operating System Support

You can install PHP on HP-UX, Linux, MacOS, OpenBSD, Solaris, Unix, and Windows operating systems. Complete operating system installation instructions are available in the PHP manual on the PHP website located at http://www.php.net/.

Web Server Support

You can install PHP on a wide range of web servers including Apache, Netscape, iPlanet, Caudium, fhttpd, OmniHTTPd, Oreilly Website Pro, Sambar, and Xitami. PHP can also be custom built for other web servers if needed.


Exploring PHP-Related Software


PHP supports a wide range of other software. There are many key elements to any successful PHP-driven website; the following are the ones I think are the most important:

Apache web server Apache is the most widely used web server on the Internet today. PHP and Apache integrate smoothly to provide you with a free, powerful, and easy-to-use combination to run your website reliably. Apache is supported by a wide range of operating systems including Unix/Linux and Windows. Approximately 12MB of disk space is required to install the Apache web server. You can learn more about the Apache web server at httpd.apache.org.

MySQL database server Combined with PHP, MySQL gives you the ability to store, edit, delete, and format information using a database with the PHP MySQL functions. You can download a free copy of the MySQL database server at http://www.mysql.org/.

MySQL Database Management Tools

There are many great tools for managing your MySQL databases. Here are a few I use every day:

phpMyAdmin phpMyAdmin is a free complete set of PHP scripts you can download from the project's website and install on your web server to manage virtually every aspect of your MySQL server. With phpMyAdmin, you can do the following:

  • Create databases, tables, and users

  • Search, browse, insert, and delete data

  • Export and import data files

  • View MySQL usage statistics

  • Reload/flush your MySQL server

You can download phpMyAdmin for free at http://www.phpmyadmin.net/.

SQLyog SQLyog is a Windows-based application that allows full control over your MySQL server. Its features are similar to phpMyAdmin, except you access it as an application rather than through your web browser. You can download a trial version at www.webyog.com/sqlyog/download.html.

PHP Editors

More than a handful of editors can handle PHP editing. PHP is a text-based scripting language that does not require any special compiling or building to create a script. You may use an editor as basic as Notepad (for Windows) or VI (for Linux), or you may use advanced text editors such as UltraEdit-32 or jEdit. Let's discuss a few of those editors now:

Notepad Notepad is as simple as it gets and comes preinstalled on Windows operating systems. I do not recommend editing your PHP scripts with Notepad because it does not come with any special features to make your code easier to understand.

UltraEdit-32 UltraEdit-32 is an advanced text editor, which includes PHP and HTML syntax highlighting. With UtraEdit-32, you have the ability to create templates to use over and over again, the ability to perform advanced search and replace functions, and the ability to search and replace criteria on entire directories at the same time. The latter is very useful! User contributed add-ons are available. A trial version of UltraEdit-32 is available at http://www.ultraedit.com/.

jEdit jEdit is an advanced text editor. It includes PHP and HTML syntax highlighting. It has the ability to "skin" or create custom themes for the editor. Because jEdit is Java based, you must have Java Runtime Environment (JRE) installed. Another useful feature of jEdit is that it gives you the ability to collapse function brackets to condense the view of your code in the editor. User contributed add-ons are available, and jEdit itself is free software. See http://www.jedit.org/ for more information.

Dreamweaver MX Dreamweaver MX is an advanced HTML WYSIWYG (What-You-See-Is-What-You-Get) editor and has the ability to generate record set MySQL queries. Dreamweaver MX supports various database formats. It is commercial software and is quite expensive, around $399. You can get more information at http://www.macromedia.com/.

There are many editors out there, and I have given you some information about the editors I have used. The bottom line is if you can type directly into the PHP script as if it were a text document, then your editing application is serving its purpose. The syntax highlighting, function collapsing, templates, and so on are just bonuses. Find something you feel comfortable using and stick with it.

Once you have found a good editor, you are ready to begin coding. Let's begin your first PHP script.


Working with PHP

To begin working with PHP, you'll need to ensure you have a web server installed and configured to use PHP. The PHP manual has complete installation instructions to set up PHP for the most popular web servers available.

For developmental purposes, I prefer to install Apache web server and PHP on either Linux or Windows, depending on where I am developing. At home, I use a dedicated Linux server that is similar to the production environment I use. This method ensures maximum compatibility between the development server and the production server and dramatically reduces headaches when it is time to push a project onto the live production server. If I am traveling, I configure Apache with PHP on a laptop and use Windows. Either way, if you use good developing habits, you should not have any issues when going live with your project.

What's Next?

This chapter introduced PHP, gave a history lesson in PHP, and introduced strings, variables, functions, error handling, error trapping, and output buffering.

Now that you have looked at the beginning of coding PHP and where it can take you, let's discuss methods of planning your projects. The next chapter covers some basic concepts involved with coming up with an idea and planning to develop a project.

No comments yet