Evolution Of A Super Hero The release of PHP 4.0, the long-awaited, next generation of the popular Web scripting language, is just around the corner. The most recent version, release candidate 1, is stable and ready to take over PHP 3.0's role in Web application development. The release takes PHP to new levels, with increased performance, more features, and cleaner syntax. Fresh Features With over 1000 functions, there's no itch you can't scratch with PHP--even with the older 3.0 version. Indeed, PHP is the most popular Apache module according to an E-soft survey. Netcraft studies indicate its continuing growth, and that it was being used on over 1.4 million domains in February 2000. Version 4.0 offers increased performance (boosts of up to 1200 percent aren't uncommon), and a much cleaner base engine. For developers, this means applications scale better, more features are available, and you can use PHP for business-critical applications. And the best of all, all those benefits are immediately available to you once you install the new version. Indeed, PHP 4.0 is backward compatible with earlier versions, except in a few cases. The most important incompatibilities are outlined at www.php.net/version4/incompatibilities.php. Most developers wlll be able to switch to the new version without having to change a single line of code. Still, perform appropriate testing before rolling PHP 4.0 out in a production environment! Once installed, the most visible changes are the new Application Program Interfaces (APIs). One of the most eagerly awaited new features is the integrated session support. With PHP 3.0, you had to use libraries like PHPLib (or write your own) to make variables persistent across multiple page requests. PHP 4.0 introduces a very flexible, yet easy-to-use API to manage sessions. To register a session variable, all you have to do is to pass it to session_register(). A call to session_register("varname") adds the variable $varname to the session's persistent data pool. The session API uses an abstracted data-handling mechanism, which allows you to store session data in different ways. Plain-text files in the /tmp directory are the default storage repository and shared memory is available for sites with high performance requirements. Because you can write your own storage modules in standard PHP and plug them into the session API, you can also use a database like MySQL, a Web Distributed Data Exchange (WDDX) repository, or other mechanisms to store your session data. The COM support has also been improved and allows instantiation of more sophisticated components now. You can even access Distributed COM (DCOM) components. You create new instances of a COM component with the predefined COM class by passing the component's name as constructor. This method makes use of a feature introduced with the new, cleaner PHP core engine: object overloading. The COM class is internal to PHP, but you can use it like any custom class -- the details of accessing COM remain completely hidden. Overloaded objects are just one of the many enhancements behind the curtains. Better Base While PHP 3.0 was a chaotic mix of programming language, syntactical add-ons like object-oriented programming (OOP), and external modules, PHP 4.0 is cleanly separated into three parts. The core engine is only responsible for parsing PHP code, and as such defines the syntax of the programming language. The core is named Zend, a combination of the two main developer's first names, Zeev and Andi. The second part is the Server API (SAPI), which handles communication and interfacing with the Web server. This makes it easy to integrate PHP with other servers. For example, you can now use PHP as an ISAPI module for Internet Information Server (IIS) -- no slow CGI binaries necessary! The third and probably most important part contains the function modules: MySQL, XML, IMAP, and so on. In accordance with this separation of tasks, the parser engine was completely rewritten. The most significant change is its speed -- it's 5 to 200 times faster than PHP 3.0. The parser has gone from analyzing and executing a script line-by-line to an approach that parses the whole script at once and executes it only after that initial analysis. If there was a parse error somewhere in the middle of your script, PHP 3.0 executed the script up to that point -- PHP 4.0 refuses to do anything with broken scripts, and will display an error message instead. The precompiling approach paves the way for a range of new products. The optimizer, for example, available free from the Zend.com Web site, performs on-the-fly optimization of common syntax constructs -- again boosting your script's performance by around 100 percent. The same goes for the bytecode compiler, a planned commercial add on by Zend Technologies. The compiler transforms your scripts into a bytecode representation to make selling and closed-source distribution easier, which wouldn't have been possible without the drastically changed base architecture of PHP 4.0. Because the the Zend engine core is completely separate from the rest of PHP, you can expect to see PHP syntax used for more than Web development in the future. There are discussions underway for integrating it into MySQL as a language for stored procedures, and for using it as a scripting language in the K Desktop Environment (KDE). Why not? Wouldn't it be convenient to write macros in your favorite word processor using the PHP syntax you're already used to? While Zeev, Andi, and the rest of the Core Group were cleaning up the backend, they also developed a handful of useful language enhancements. For starters, the OOP in PHP was improved to include more straightforward inheritance, better introspection, and full support for serializing class instances. The developers introduced output buffering that allows you to access and modify page content even after the content has been generated. For example, you could use this to add proper indentation to HTML source generated by the script. The namespace handling was also extended. Until version 4.0, form variables passed by GET, POST, or cookies were always added automatically to the global namespace. While this makes PHP easy to use for simpler scripts, it can be confusing with large applications. Did a value come from a form, was it passed by the URL, or was it stored in a cookie? PHP 4.0 allows you to turn off this behavior (through the register_globals switch in the configuration), so you're forced to access all variables through their corresponding namespace arrays, like $HTTP_GET_VARS or $HTTP_COOKIE_VARS. This can add considerable discipline to your programming style. PHP of Your Dreams In daily work, you notice the small details that make a programming language difficult or easy to use. PHP 4.0 not only strengthened its base, but also took note of the details. It seems like someone collected a list of common feature requests and wishes for PHP 3.0, then implemented them in PHP 4.0. Many small yet disturbing problems with version 3.0 have been solved in the new version. To list a few enhancements in 4.0: You can pass multi-dimensional arrays from HTML forms, by using the notation: . You can use variable function arguments, using the func_get_args() function. The serializer supports classes. Perl-style here-docs are at your disposal. The predefined arrays $HTTP_GET_VARS and $HTTP_POST_VARS are protected and can't be overridden by values from the Web. The handling of empty and false-value variables is clearer; a new comparison operator, in the form of three equals signs (===), has been introduced, which checks for type matching and equality. And the list could go on. Even with all these enhancements, there's no sign that development is slowing down. In fact, other features are already waiting for their debuts. Next in line is support for LibXML, Gnome's XML parser, which gives XML documents a DOM-like interface. The API is currently being developed and is even usable, but it probably won't achieve stable classification in time for the 4.0 release. Another interesting new development is the PHP Extensions and Add-ons Repository (PEAR). Similar to Perl's CPAN, PEAR provides a standardized way to distribute code packages. It comes complete with a means of installing the packages, associating meta information with them (such as description or author name), and establishing naming and coding conventions. This makes library reuse and distribution easier. To summarize, PHP has matured significantly. It's ready for the most sophisticated tasks in Web application development -- and for those beyond Web development, when the Zend engine is incorporated into other products. So stay informed; PHP's future looks promising. Article written by Tobias Ratschiller