An Introduction to PHP

Every user has his or her own specific reason for using PHP to implement a mission-critical application, although I find that such motives tend to fall into what I like to call the four pillars of PHP: Practicality, Power, Possibility, and Price.
Practicality

From the very start, the PHP language was created with practicality in mind. After all, Lerdorf’s original intention was not to design an entirely new language, but to resolve a problem that had no immediately applicable solution. Furthermore, much of PHP’s early evolution was not the result of the explicit intention to improve the language itself, but rather to increase its utility to the user. The result is what I like to call a minimalist language, in terms of what is required of the user, and in terms of the language’s syntactical requirements. For starters, a useful PHP script can consist of as little as one line; there is no need for the mandatory inclusion of libraries like in C. For example, the following represents a complete PHP script, the purpose of which is to output the current date, such as March 16, 2004:



Another example of the language’s penchant for compactness is its ability to nest functions. For example, I can effect numerous changes to a value on the same line by stacking functions in a particular order, in this case producing a pseudorandom string of five alphanumeric characters, such as “a3jh8”:

$randomString = substr(md5(microtime()), 0, 5);

PHP is a loosely-typed language, which also increases its practicality. For example, there is no need to explicitly create, typecast, or destroy a variable, although you are not prevented from doing so. PHP handles all of this for you, creating variables on the fly as they are called in a script, employing a best guess formula for automatically typecasting variables, and finally, automatically destroying variables and returning resources back to the system when the script completes. In these and many other respects, the language allows the developer to concentrate almost exclusively on the final goal (a working application), attempting to handle many of the administrative aspects of programming internally.
Power

In the earlier introduction to PHP 5, I alluded to the fact that the new version is more qualitative than quantitative in comparison to previous versions. Previous major versions were accompanied by enormous additions to PHP’s default libraries, to the tune of several hundred new functions per release. Presently, 113 libraries are available, collectively containing well over 1,000 functions. Although you’re likely aware of PHP’s ability to interface with databases, manipulate form information, and create pages dynamically, did you know that PHP can:

* Create and manipulate Macromedia Flash, image, and Portable Document Format (PDF) files

* Evaluate a password for guessability by comparing it to language dictionaries, and easily broken patterns

* Communicate with the Lightweight Directory Access Protocol (LDAP)

* Parse even the most complex of strings using both the POSIX and Perl-based regular expression libraries

* Authenticate users against login credentials stored in flat files, databases, and even Microsoft’s Active Directory

* Communicate with a wide variety of protocols, including IMAP, POP3, NNTP, and DNS, among others

* Communicate with a wide array of credit-card processing solutions

Of course, I’ll strive to cover as many of PHP’s interesting and useful features as possible in the coming chapters.

Possibility

PHP developers are rarely bound to any single implementation solution. On the contrary, a user can easily be overwhelmed by the number of choices offered by the language. For example, consider PHP’s array of database support options. Native support is offered for over twenty-five database products, including Adabas D, dBase, Empress, FilePro, FrontBase, Hyperwave, IBM DB2, Informix, Ingres, Interbase, mSQL, direct MS-SQL, MySQL, Oracle, Ovrimos, PostgreSQL, Solid, Sybase, Unix dbm, and Velocis. In addition, abstraction layer functions are available for accessing Berkeley DB-style databases. Finally, two database abstraction layers are available, one called the dbx module, and another via PEAR, titled the PEAR DB.

PHP’s powerful string-parsing capabilities is another feature indicative of the possibility offered to users. In addition to more than eighty-five string manipulation functions, two distinct regular expression formats are supported POSIX and Perl-compatible. This flexibility not only offers users of differing skill sets the opportunity to immediately begin performing complex string operations, but also to quickly port programs of similar functionality (such as Perl and Python) over to PHP.

Do you prefer a language that embraces functional programming? How about one that embraces the object-oriented paradigm? PHP offers comprehensive support for both. Although PHP was originally a solely functional language, the developers soon came to realize the importance of offering the popular OOP paradigm, and took the steps to implement an extensive solution.

The recurring theme here is that PHP allows you to quickly capitalize on your current skill set with very little time investment. The examples set forth here are but a small sampling of this idea, which can be found repeatedly throughout the language.
Price

Since its inception, PHP has been without usage, modification, and redistribution restrictions. In recent years, software meeting such open licensing qualifications been referred to as open-source software. Open-source software and the Internet go together like bread and butter. Open-source projects like Sendmail, Bind, Linux, and Apache all play enormous roles in the ongoing operations of the Internet at large. Although the fact that open-source software is available free has been the characteristic most promoted by the media, several other characteristics are equally important if not more so:

* Free of licensing restrictions imposed by most commercial products. Open Source software users are freed of the vast majority of licensing restrictions one would expect of commercial counterparts. Although some discrepancies do exist among license variants, users are largely free to modify, redistribute, and integrate the software into other products.

* Open development and auditing process. Although there have been some incidents, open-source software has long enjoyed a stellar security record. Such high standards are a result of the open development and auditing process. Because the source code is freely available for examination by anyone who wants to, security holes and potential problems are rapidly found and fixed. This advantage was perhaps best summarized by open-source advocate Eric S. Raymond, who wrote, “Given enough eyeballs, all bugs are shallow.”

* Everyone is free to participate. Development teams are not limited to a particular organization. Anyone who has the interest and the ability is free to join the project. The absence of member restrictions greatly enhances the talent pool for a given project, ultimately contributing to a higher quality product.

Summary

This chapter has provided a bit of foreshadowing about this wonderful language to which much of this book is devoted. We looked first at PHP’s history, before outlining version 4 and 5’s core features, setting the stage for later chapters.

In Chapter 2, prepare to get your hands dirty, as you’ll delve into the PHP installation and configuration process. Although readers often liken most such chapters to scratching nails on a chalkboard, you can gain much from learning more about this process. Much like a professional cyclist or race car driver, the programmer with hands-on knowledge of the tweaking and maintenance process often holds an advantage over those without, by virtue of a better understanding of the software’s configurable behaviors and quirks. So grab a snack and snuggle up to your workstation; it’s time to build.