I l @ ve RuBoard |
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-Processor ”you 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...". [1] In English, this means that PHP code is typically embedded within HTML documents; this code ” variables , functions, commands ”is 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.
That's not all, though ”PHP 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. Language FeaturesPHP 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:
Listing 1.3 demonstrates some of these features. Listing 1.3 A simple PHP script<?php ////////////////////////////////////////////////////////////// // // 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 // ////////////////////////////////////////////////////////////// // includes // DB variables are sourced from db_config.php include("db_config.php"); include("TemplateClass.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 session_start(); // register the user's ID as a session variable session_register("SESSION_UID"); list($id, $username, $nickname) = mysql_fetch_row($result); $SESSION_UID = $id; $welcome_msg = "Welcome back, " . $nickname; // close connection mysql_close($connection); // create an instance of the Template object $obj = new Template("welcome.tmpl", "#000000", "#FFFF00"); // and execute some class methods $obj->set_welcome_msg($welcome_msg); $obj->print_template(); } else // login/pass check failed { mysql_close($connection); // redirect to error page header("Location: error.php?ec=0"); exit; } ?> 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. ApplicationsIn 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. |
I l @ ve RuBoard |