X Home X X News X X Apps/Tools X X Support X X Downloads X X Articles X X Contact X
An Introduction to PHP Version 4
Share |

An Introduction to PHP Version 4

Craig Knudsen (craig@k5n.us)
01 Jun 2000

PHP4 comes as the PHP platform is continuing to grow in popularity. An exciting new set of features will have web developers itching to convert their old apps from PHP3 and start taking advantage of PHP4's new tools. This article provides an overview of what PHP is, where it came from, and where it is going. An overview of new features is included along with instructions on installing PHP4.

PHP Version 4

I first discovered PHP a little over a year ago at Freshmeat.net, the popular open source application portal now owned by Andover.net. Because most non-commerical sites used Perl/CGI for dynamic content, I found it interesting that Freshmeat's dynamic pages all ended with ".php3" instead of the ".pl" or ".cgi" associated with Perl/CGI sites like Slashdot.org. I did a little exploring, quickly downloaded PHP version 3, and immediately became a PHP fan.

So, what is PHP? It's described as an open source server-side HTML-embedded scripting language by its developers, but this definition will likely evolve over time. In addition to HTML, PHP can be embedded within XML or even plain text. And, while it is currently only used within a Web server environment, PHP may expand to other areas like RPC in the not too distant future.

PHP is similar to JavaServer Pages (JSP) and Microsoft's Active Server Pages (ASP). All three of these tools for generating dynamic Web content allow embedding commands within the HTML, creating documents that are a combination of HTML page layout and server-side scripting code. JSP uses Java as its programming language while ASP allows both VBScript and JScript (Microsoft's version of JavaScript). PHP uses its own programming language with syntax and features derived mostly from Perl, Java and C.

PHP differentiates itself from ASP and other Web scripting tools by using the open source development model. It's developed by a set of six core developers and a large group of contributors. Official releases are available as source code via anonymous ftp, and the current code is always accessible to the world through a public CVS server.

PHP is typically run as a module to the popular Apache web server, meaning that the PHP engine is compiled into the Apache server's excutable. The alternative is to build PHP as a CGI and configure the web server to handle ".php" files with the PHP executable, much like Perl can be configured to handle files with the ".pl" extension. Running as an Apache module is the preferred option and will give significantly better performance.

From PHP/F1 to Zend

The first version of PHP (which stood for "Personal Home Pages") was developed by Rasmus Lerdorf in 1995, before the days of JSP, ASP, and mod_perl. Lerdorf released the first code, a set of Perl scripts, to the public as freeware. (The term "open source" would not be coined until 1998.)

After rewriting the Perl code in C, adding support for handling forms, and adding MySQL support, PHP/F1 was released in 1996. Others began to contribute code including support for accessing other databases, and PHP/F1 grew into PHP version 2.

While using PHP 2 to develop applications, developers Zeev Suraski and Andi Gutmans began running into problems with PHP's limitations. After examining the parsing code in PHP 2, Suraski and Gutmans decided to join the PHP development effort and rewrite the scripting engine. Their intent was to improve the design, performance and reliability for the 3.0 release. Version 3 was widely successful and continued to grow.

As developers began creating increasingly complex sites with PHP 3, it become clear to Suraski and Gutmans that their design for the scripting engine was geared towards faster performance on shorter scripts. As version 3 attracted more users, other issues began to surface. Because it was not thread-safe, PHP could not be integrated into multi-threaded web servers such as Netscape's Enterprise Server, Microsoft's Internet Information Server (IIS) and the Windows port of Apache. In order to be competitive with other web scripting tools, PHP would need to be able to integrate seamlessly with non-Apache servers.

Suraski and Gutmans set out to rewrite the scripting engine from scratch again. In addition to improving performance, the scripting engine needed to be thread-safe, compatible with PHP 3 (so PHP 3-based sites could easily switch upgrade to version 4), independent of PHP (so the scripting engine could be used for other applications), and extensible.

The Zend scripting engine was the result of their work. ("Zend" is derived from their first names, Zeev and Andi.) In the process, Suraski and Gutmans founded Zend Technologies Ltd., based in Isreal. The company's goal is to provide support and services for the PHP community including the development of commercial tools. Their first product, the Zend Optimizer, can improve PHP application performance up to 100%. A beta version of the Optimizer was made available in March as a closed source binary for glibc 2.1 systems.

PHP's Cool Features

It's hard to decide where to start with PHP's features because it can do so much. Let's start with ease of use. You can have your first PHP-based dynamic web page created in no time. PHP shares some handy features with Perl. You aren't required to declare variable before using them nor do you need to specify what type a variable is (array, integer, etc.) Like Perl, PHP will decide at runtime for you.

There's no complicated IDE required for PHP nor are you subject to compiling code from the command line. Scripts are compiled at runtime and errors are displayed in your web browser. If you're an occasional Windows user, you have the option of using Microsoft's FrontPage editor (if you configure Apache with FrontPage support).

Good documentation is critical to learning a new language or API. The PHP home page provides a novel approach to this by borrowing concepts from the open source community. The online manual documents the PHP API and syntax while allowing users to read and post comments and questions on each page.

A vast number of both free and commercial databases are supported. The most popular database used with PHP is MySQL, which is basically free. (While the most recent releases may not be technically "open source" as defined by the Open Source Initiative, there is an older version available under the GPL.)

Other supported databases include Oracle, Informix, Sybase, mSQL, InterBase, and PostgreSQL. PHP does not provide a common API for accessing databases. Each supported database has its own unique API. For instance, to connect to a MySQL database, you would use the mysql_connect function. To connect to Oracle 8, you would use the OCILogon function.

Having unique APIs for each database allows PHP to provide developers with the full set of capabilities for each database rather than providing a set of minimal functions that all databases support. Users that need a common database interface can take advantage of the ODBC API for databases that have ODBC drivers. Although ODBC is not a standard component in most Linux distributions, ODBC drivers are available for most databases.

Opening and closing database connections can take longer than any other portion of a dynamically generated page. PHP's database pooling allows connections to remain open after a page is finished being processed. The next page that requests a database connection will then receive the already open connection, dramatically improving system performance. The concept is very similar to HTTP 1.1's keep-alive feature used to speed-up web page downloading.

In order to use connection pooling, you must be running PHP as a module rather than a CGI. When run as a CGI, the PHP engine exits when finished processing the page, making it impossible to keep a connection open.

PHP's capabilities extend far beyond accessing databases. Other features include generating images and PDF documents dynamically as well as accessing LDAP and IMAP servers. (See sidebar on PHP features for a more complete summary.)

Portability is another of PHP's strengths. Developers can use Linux for their development machines and know that their applications will work identically on Linux, Solaris or even Windows NT. Microsoft IIS support will improve with version 4 by providing an ISAPI version, giving Microsoft IIS users the same benefits that Apache users have had since version 3.

Because it is an open source project, many developers have contributed extensions to PHP. Version 3 extensions are typically compiled in during installation. Version 4 aims to encourage extensions by making it easier to dynamically load extensions.

Most extensions add support for third-party software. For example, Oracle client libraries are required to include the Oracle API in PHP.

New in Version 4

There's a good deal of excitement about the expected release of PHP 4.0. The first public beta of PHP 4 debuted in July of 1999, and the most recent (and probably last) beta was released this February (Beta 4 patch 1).

Version 4 will probably be best know for its speed improvements over its predecessor, a direct result of Zend's new high-performance scripting engine.

The Zend scripting engine is actually licensed separately from the rest of PHP. Eventually, Zend Technologies hopes to use their scripting engine in other products.

PHP 4 will be distributed under a different license than PHP 3, which uses the GPL. The Zend scripting engine's license states that it cannot be used in part of a commercial product. You can still sell a PHP-based application and bundle PHP 4/Zend with it. However, you cannot create a new product that uses the Zend library directly, such as a macro processor for a commercial word processor. The Zend license is very similar to Troll Tech's QPL.

The most frequently requested new feature was HTTP session support. Sessions allow developers to store state information (such as items in a user's shopping cart) despite the fact that HTTP is a stateless protocol. While session support was available to PHP 3 developers through an add-on library called PHPLIB, PHP 4 will include native session support.

Sessions are typically maintained through the use of HTTP cookies. The server generates a unique session identifier when a user first visits the site and sends it to the user via a cookie. The user's browser will then include this session id in all future requests to the server. Of course, this only works until users disable cookies in their browser after reading about DoubleClick's efforts to track their surfing across the web. Fortunately, PHP can support sessions without using cookies as well.

The build system for PHP 4 on Unix has been rewritten as well. The process of building dynamically loadable extensions has been greatly simplified.

PHP is most commonly used as an Apache module. Up until recently, Apache was your only option unless you ran PHP as a CGI. PHP 3 was designed as a single-threaded Apache module. However, PHP 4 abstracts the web server integration and adds thread safety.

PHP 4 will be able to plug into new servers including Microsoft IIS (using ISAPI), Netscape's servers (using NSAPI), AOLServer and a few others. This will drastically improve performance on these servers and allow them to take advantage of database connection pooling support. Work is also under way to allow PHP to be run as a Java servlet.

Version 4 maintains almost complete compatibility with version 3, allowing almost all PHP 3 scripts to work unmodified under PHP 4. While incompatibilities do exist, they involve seldom-used features. For example, the scope of break and continue is now local to an include file, a change that will have little impact.

Coming Soon

PHP 4.0 will likely be released in the second quarter of 2000. How soon will PHP 3-based sites upgrade? Given the lengthy duration of the beta cycle, version 4.0 should be a very stable product for sites using either the Apache module version or the CGI version. As more users test the new support for Microsoft's IIS ISAPI, AOLServer, and Netscape's NSAPI, you can expect regular updates to be posted.

Having used PHP 3 for over a year now, I'm looking forward to version 4 as both a web developer and open source advocate. While some could argue that PHP is already an open source success story, it doesn't yet have the name recognition of ASP or JSP in the commercial world. Version 4's improved performance and support for new platforms could provide the push that makes PHP one of the next big open source success stories.


PHP Manual
PHP Knowledge Base
Zend Technologies Ltd.
PHP 4.0 & Zend License FAQ


Feel free to provide feedback on this article.

About the Author

Craig Knudsen has been developing software solutions since the early 90s. He founded the open source WebCalendar project in 2000 and continues to lead the project. He has developed web-based applications using all sorts of combinations of C, Perl, Java, JavaScript, PHP and even ASP. When not attempting to build furniture in his workshop, you can reach him at craig@k5n.us.
Valid XHTML 1.0 Valid CSS PHP
Page Last Updated: 22-Aug-2010
Copyright ©2004-2018 k5n.us, All Rights Reserved