Let's move on to PHP, easily my favorite web development language. This section provides a brief overview of PHP's history, capabilities, and applications.
PHP (the name actually is a recursive acronym for PHP: Hypertext Pre-Processoryou know how geeks are!) began life as a glint in the eye of Rasmus Lerdorf in late 1994. (For the full story, visit http://www.php.net/manual/phpfi2.php#history.) Rasmus needed a tool to log visitors to his personal web page; his solution, using a C wrapper and special tags embedded into the HTML code, became the first version of PHP.
PHP hit the big time between 1997 and 2000 as more and more sites adopted it. The release of PHP 4.0 in late 2000 only served to widen its fan base. As a complete rewrite of the PHP engine, PHP 4.0 is faster, more scalable, and more full-featured than any of its predecessors. It is in use on more than six million web sites today, and that number is climbing rapidly.
Still wondering what exactly PHP is? Well, to quote its authors, PHP is "...a serverside HTML-embedded scripting language...". In English, this means that PHP code is typically embedded within HTML documents; this codevariables, functions, commandsis interpreted and executed by the server, with the resulting output returned to the browser. Special PHP tags are used to distinguish between PHP commands and regular HTML markup.
 Stig Sæther Bakken, Egon Schmid, et al. PHP Manual. Available from Internet: http://www.php.net/manual/en
That's not all, thoughPHP also happens to be the most fun language I've had the opportunity to work with over the past few years.
Unlike other scripting languages, which seem to delight in convoluted syntax and tortuous structures, PHP code is clean, easy to read and understand, and incredibly user-friendly. The language comes with great documentation, a friendly and knowledgeable user community, and a feature set that would turn most other web scripting languages green. And its development team (more than 300 people across the globe) is constantly innovating and adding support for new technologies, thereby ensuring that it's on the cutting edge of new technology. What's more, PHP's huge library of built-in functions ensures that even the most complex tasks can be accomplished in less time than you thought possible.
All this, of course, would come to naught without the active support of realworld developers. Fortunately, PHP has that too. The latest Netcraft survey (http://www.netcraft.com/survey/) reveals that PHP usage is growing at the rate of more than 20 percent per month, with over 40 percent of the web's Apache servers running PHP. This widespread acceptance can only be music to the ears of PHP's many devotees, who have elevated PHP's developers to the status of gods and the language itself to the forefront of the popularity sweepstakes.
PHP is a remarkably full-featured programming language, making it the ideal tool for developers looking to build complex, high-traffic web sites. Here's why:
PHP distributions are available for a wide variety of platforms, including Windows, Macintosh, OS/2, and most flavors of UNIX, including Linux. Because PHP code written on one platform usually works "as is" on any other supported platform, using PHP can significantly reduce development time in a multiplatform environment. (Some PHP functions are platform-specific.)
Independent evaluations have demonstrated that PHP is faster and more reliable that most other scripting languages. Both Fortune 500 companies and Mom-and-Pop businesses use it to run their web sites. And the completely reworked engine in PHP 4.0 raises the performance bar even further. Zend Technologies, Ltd. (http://zend.com/zend/aboutphp.php), which played a pivotal role in the development of PHP 4.0, claims a 50-fold performance increase over previous versions; the reality is a little less staggering, but still impressive. (For numbers and benchmarks, visit the research done at http://www.linuxplanet.com/linuxplanet/reviews/1891/2/.)
Ease of use.
PHP is easy to learneven a novice developer can pick up the basics in a few hours, and there are many online tutorials and books available to speed up the process. (I've even written a few myself. See the resources listed at http://www.xmlphp.com or http://www.newriders.com for more information.) PHP code is elegant and easy on the eyes, and the language comes with extensive documentation and usage examples. Because PHP is so easy to use, but packs so much power under its unassuming surface, it's become extremely popular as a tool for rapid application development, with more and more developers using it to construct high-traffic web sites and complex web-based applications.
PHP is a child of the open source communityits source code is freely available, and developers are encouraged to make their own contributions to it. This open source approach has been the single most important factor responsible for the widespread acceptance of PHP all over the world. By sharing the code with other developers, the PHP development team has been able to stay abreast of current technologies and incorporate them into the language faster than their more traditional counterparts. And this open source approach has also given rise to a network of skilled PHP developers, who throng mailing lists and chat rooms with questions, comments, and knowledgeable advice.
Support for new technologies can easily be added to PHP through its modular extensions, which make it possible for developers to quickly incorporate new features and capabilities into their PHP build. Extensions available today allow developers to use PHP to perform FTP, IMAP, and POP3 operations; dynamically generate GIF, JPEG, and PNG images, Shockwave Flash files and Adobe PDF documents; validate credit card transactions; perform encryption of sensitive data; connect to Java; and process XML-encoded data.
With most of today's web pages built dynamically from a database, PHP, with its out-of-the-box support for a variety of different databases, comes as a breath of fresh air. PHP comes with built-in support for MySQL, the extremely popular open-source database system, and can easily be configured to support a variety of other databases, including IBM DB2, mSQL, Oracle, PostgreSQL, Sybase, dBase, and ODBC. And, to simplify things even further, PHP 4.0 includes a database abstraction layer (conceptually similar to Perl's DBI module) that makes it possible to easily migrate from one DBMS to another.
Classes and objects have been available to PHP developers since version 3.0 of the languages; however, PHP's OO support has always been a little incomplete.Version 4.0 takes PHP closer to being a true object-oriented language. Among the new features, you'll find operator overloading, object nesting, and reference counting.
Built-in session management.
Unlike many other languages, PHP comes with native session management support, making it possible to track individual client sessions on a web site and create web applications that respond to the needs of individual users. With personalization features now almost de facto on popular web sites, PHP's built-in session management can significantly reduce both the time spent on developing and testing such sites.
Listing 1.3 demonstrates some of these features.
Listing 1.3 A simple PHP script
// this is a simple login script that accepts a username
// and password and validates them against a database
// if the validation is successful, a new session is
// created and a customized template generated
// if the validation fails, the user is redirected
// to an error page
// DB variables are sourced from db_config.php
// check login and password
// connect and execute query
$connection = mysql_connect
($hostname, $user, $pass) or die
("Unable to connect!");
$query = "SELECT id, username,
nickname FROM user WHERE
username = '$frmuser' AND
password = PASSWORD('$frmpass')";
mysql_select_db($database) or die
("Unable to select database!");
$result = mysql_query($query) or die
("Error in query: $query. " . mysql_error());
// if row exists - login/pass is correct
if (mysql_num_rows($result) == 1)
// initiate a session
// register the user's ID as a session variable
list($id, $username, $nickname) = mysql_fetch_row($result);
$SESSION_UID = $id;
$welcome_msg = "Welcome back, " . $nickname;
// close connection
// create an instance of the Template object
$obj = new Template("welcome.tmpl", "#000000", "#FFFF00");
// and execute some class methods
// login/pass check failed
// redirect to error page
Listing 1.3 demonstrates many of the most common PHP features. PHP's native MySQL support simplifies the task of connecting to a database to validate the user's account, although the built-in session functions make it easy to retain important user information as the user browses from page to page within the site. Object support is demonstrated by the use of a custom Template object, which creates the final web page seen by the user and provides developers with a robust and portable mechanism for page generation throughout the site.
In the old days, web sites consisted of static HTML pages with a few images thrown in for variety. That is no longer the case: today's web sites are complex and demanding, with movie-quality animation, high-res audio and video, and web pages built on-the-fly from immense databases churning away in the background. And, as you might imagine, PHP's ease of use, performance, and proven track record ensure that the language's primary application lies in the field of these dynamic, high-traffic web sites and applications.
PHP is well-suited to such complex web applications for a number of reasons. The language's built-in database capabilities ensure that it can connect easily to a variety of different databases for dynamic page generation; its payment processing and validation capabilities make it a good choice for online commerce; and its portability across platforms and support for important web technologies like Java and Flash make it suitable for a diverse range of other applications.
Today, PHP is used by businesses (such as Mitsubishi and Ericsson), government organizations (such as the United States Naval Research Laboratory), and popular open-source portals (for example, freshmeat.net and phpbuilder.com) for applications ranging from content management to wireless data transfer. This fact again demonstrates the versatility of the language.
If you're interested in PHP's numerous applications for business, you'll find some very interesting case studies at http://www.zend.com/zend/cs/.
I'm not going to get into the details of PHP's syntax and structure here, but will assume you know the basics. If you don't, you should take a look at PHP Functions Essential Reference by Zak Greant, Greame Merral, Torben Wilson, and Brett Michlitsch (New Riders Publishing, 2001) ISBN: 073570970X.