An open-source alternative to ASP
[Author's Note: PHP's Internet Server API (ISAPI) module is a new feature in PHP 4.0. Earlier versions of PHP were available only as Common Gateway Interface (CGI) executables. Although the ISAPI option is a significant leap forward for PHP, using the ISAPI version instead of the CGI version presents security and reliability concerns. (For information about these concerns, see PHP's README file.) Until the ISAPI version matures, I recommend that you use the CGI version of PHP in your production environments.]
Have you been to a Web site recently and noticed files ending with .php or .phtml extensions? You might already know that these extensions denote PHP files. You might even know that PHP stands for PHP Hypertext Preprocessor. But what is PHP? In short, PHP is an open-source alternative to JavaServer Pages (JSP), CGI scripting languages, Allaire's ColdFusion, and Microsoft's popular Active Server Pages (ASP).
Microsoft succeeded with ASP in part because ASP is easy to learn and implement and is tightly integrated with a reliable, high-performance Web serverIIS. PHP shares these attributes with ASP, but it adds portability. PHP is available on a variety of Web servers and OSs. PHP is also a great way for Microsoft-focused Web professionals to get acquainted with open-source software without changing anything about their current architecture or environment. To get acquainted with PHP, let's
- take a brief first look at PHP, including its benefits and a quick comparison with ASP
- review the steps required to install and configure PHP on IIS
- go over a practical example involving ODBC database publishing that you can put into production
First Look
PHP isn't a scripting engine but rather a scripting language like VBScript or JScript. PHP uses the Zend scripting language engine. PHP was developed as open-source software that used both Perl and UNIX scripting languages as a model. PHP is essentially HTML interspersed with code that script delimiters identify. PHP's well-documented API contains an extensive range of functions (or methods), including math functions, XML parsing, file and directory operations, and email handling. Other features, such as session support and include files, are also available.
Using library (API) functions that are available for all common commercial databases, PHP lets you interact directly with databases. For example, to establish a connection to a Microsoft SQL Server database, you can choose between a generic ODBC function or SQL Server-specific functions. One advantage of using direct database functions is that you don't have to create and maintain Data Source Names (DSNs) on your Web server. The most significant advantage is that direct database functions offer measurable performance advantages because the code that implements these functions has been tailored to interact with the database's native API. Keep in mind that if you use a database API to write your PHP code, you're effectively trading portability for performance. (You must specifically enable your database extensions in PHP's .ini file. By default, PHP supports ODBC without .ini file modification.)
PHP was developed as a Web scripting language. Although PHP will soon be extended for programming client-side programs, this capability will be too immature to use in the near future. This restriction might limit PHP's adoption by enterprises that need to reuse existing components or leverage the richer programming capabilities that COM or other commercial software development environments offer. However, if you're looking for a quick way to get reasonably powerful Web applications up and running, then consider PHP.
PHP and ASP
You can write ASP scripts in either JScript or VBScript. PHP closely resembles JScript in terms of syntax and functionality. For example, flow control in PHP and JScript are identical. JavaScript, Perl, Java, C, C++, and Microsoft's new C# all share a similar style and syntax. PHP doesn't yet meet the complete definition of an OOP language, although you can design code in an object-oriented manner. Table 1 provides a short list of the properties and features of both PHP and ASP.
PHP is a simple language to use and understand. Here are a few examples to illustrate this point. When you use HTML forms to send data to an ASP script for processing, you usually establish references to the form's data. You establish these references (or variables) differently depending on whether the form uses the HTTP POST or the HTTP GET method. To create these variables with ASP, use
Dim Price = Request.Form("Price")
if the form method is POST, and use
Dim Price = Request.Querystring("Price")
if the form method is GET.
With PHP, the HTML form data is immediately available for scripting purposes, without variable declaration. You simply use the variable $Price wherever you need to, no matter what the form method is. (PHP variables must start with a dollar sign$just like Perl. This stipulation makes it easy to identify variables within your scripts.) In fact, $Price could be a cookie value. Let's suppose you had previously set a cookie that contained the name-value pair Price=someValue. This data would be immediately available to your PHP script as $Price. Although PHP's default behavior is to build the GET, POST, and COOKIE variables automatically, this practice isn't recommended because of a potential security risk with poorly formed code. You can turn this feature off within the php.ini file. When you turn this behavior off, the Price GET variable, for example, would be accessible through $HTTP_GET_VARS["Price"]. (For information, see PHP's documentation.)
Here's another example. To display the date in the browser, you can call the date() function with arguments to control the date-display format. For instance,
echo date("m/d/Y")
displays 04/28/2001.
Benefits of PHP
As an administrator concerned with application-development strategy, you probably already use ASP to some extent. What would prompt you to incorporate PHP into your strategy or consider testing PHP? The two best reasons to consider PHP are portability and ease of use.
PHP can run on a variety of Web servers, including Apache and Netscape, which means that your code is portable, with some exceptions. For the best possible performance, PHP provides database-dependent libraries (APIs) for most commercial databases, including SQL Server, Oracle, and DB2. I recommend that you use these APIs only if you have a stable relational database management system (RDBMS) environment in which one database exists as a fixed standard for the entire term of the application's life cycle. Outside this situation, using database-dependent APIs to code an application is counterproductive and forces expensive code conversion in order to switch to a different database. PHP's ADODB or ODBC APIs are appropriate in most cases.
Probably the greatest benefit of PHP is its remarkable ease of use. ASP experience is helpful in learning PHP but not essential. PHP is a good way to introduce HTML coders to the realm of server-side scripting. I recommend that you have a solid foundation in HTML, not just visual tools such as Microsoft FrontPage or Macromedia Dreamweaver, before taking on PHP.
If you use good coding practices, PHP application performance is excellent. However, I recommend limiting PHP use to light Web applications (e.g., simple database applications, Web catalogs, email forms). A COM or Microsoft .NET solution is better for transaction-oriented or high-volume business applications.
John Holmes October 31, 2002