|Back to Papers and Articles
Web Programming with PHP - Why choose PHP?
Copyright 2001 Paragon Corporation ( December 20, 2001)
PHP is a language that has been around for a couple of years now, but has undergone significant changes over the years. PHP is a server-side, embeddable HTML language. We focus most of our attention on the PHP (4.0.6) (PHP 4.1 just came out as of this writing). In this article we will focus on its use as a web scripting language and what makes it such a popular language.
PHP borrows much of its syntax from Perl. When it was first created, it ran on-top of Perl and was created to provide a more trimmed down easier to write HTML embeddable alternative to Perl. It like other web scripting languages we have covered, can be embedded in HTML docs. This is most commonly done using the <?php ..> tag or other syntax such as the less common ASP-style <% .. %> syntax.
What makes PHP special?
Where can PHP runPHP from the ground up was designed to be an HTML embeddable language. PHP is an open-source language similar to JAVA/JSP except its under the GPL . Java/JSP opensourceness is controlled by Sun. Like Java, PHP can run on multiple platforms. You can download a PHP interpreter for Windows, Unix, Linux, and Mac - these are fairly easy to install and can run as a plug-in on top of numerous web servers such as Apache, iPlanet, IIS etc. It also has built-in robust support for various flavors of databases e.g. Oracle, MySQL, mSQL, PostGres, SQL Server, dBase Files, ODBC, ADO with persistent connection support at no extra cost.. These are all available for free and are fairly robust or come packaged with the PHP install.
Most web scripts you write in PHP will run with little to no change on another platform. For example, we tend to develop PHP web apps on Windows machines, and then upload to UNIX machines for production. We found this to be a great selling factor and in many cases superior to Java's JSP cross-platformness. We found a major failing in Java Servlet's cross--platformness. Even though the Servlet API is controlled by Sun the actual implementation is left up to the discretion of Servlet Engine providers who often add their own proprietary extensions - making it easy to get trapped into using Servlet Engine specific extensions. So although JSP and Java in general are cross-platform, you run into the perverse situation in that it does not run consistently the same even on the same platform. For example if you started using JRUN etc. or BEA - you may find it difficult to port your applications to another Servlet engine such as Tomcat or Jetty unless you paid careful attention to stear away from the very enticing proprietary tags provided by your current Servlet engine of choice. In the past the more robust Servlet engines were far from free and tended to be very pricey and have proprietary extensions that are easy to get trapped into and that do not port well to other Servlet engines if you need to port to another Engine later. This has been changing more recently with the introductions of opensource servlet engines such as Tomcat and Jetty to name a few. PHP on the other hand its actual implementation is free and there really is only one common implementation so no frustration figuring out which Servlet engine is best etc. Cold Fusion runs on various platforms as well, but the platform support for PHP seems more extensive. ASP in the past was just a Windows scripting language, but this too is changing with the arrival of ChiliASP and the upcoming GNU ASP.
The Structure of the PHP LanguagePHP is both object-oriented and procedural in nature (similar to ASP) and allows one to easily create scripted classes using a C++/Java like syntax or if you prefer procedural programming, you can create a set of usuable functions instead. This makes it very appealing to varying programming styles and for various situations where a strict OO approach is not preferred. Take for example math functions - when performing math calculations - in this situation a functional approach is preferable, but in strict OO languages such as Java, a functional approach is mimicked by stuffing functions in a class as static member functions such as the Java Math class. In PHP , ASP, or Cold Fusion you would simply just use functions instead..
PHP is not a strictly-typed language which makes it similar to ASP's VBScript/JScript and vastly different from Java strictly-typed approach. This in some cases is quite beneficial and in other situations may be an impediment. For example you can have your class methods and functions return anything and be able to use these without having to worry about casting them as you have to in Java and C++. On the other-hand, since your classes can return almost anything, you will tend to see more run-time errors if a script tries to access features of a returned object that the returned object can not do.
PHP is not inherently a tag-based language like CFML and JSP, although there are template engines such as Smarty that are downloadable for PHP that offer the separation of logic from design.
PHP like the other offerings we've discussed has built-in support for session handling, cookie handling and form handling, and this functonality has been made more secure and improved in the 4.1 release.
How much Does PHP Cost?PHP has very few hidden costs - compared to JSP/Servlets, ASP, and Cold Fusion. Most everything you need to create a powerful site is pre-packaged with the install or available as a free extension. It is remarkable how much comes packaged with the PHP implementation or which is easily available as a free add-on. Take for example PHP's built-in support for various kinds of databases, for file uploading, URL connections or its expansive string function, regular expression library, compression libraries and image rendering libraries which are failry easy to understand. To achieve much of this functionality with JSP/Servlets, ASP, or Cold Fusion requires buying a sometimes pricey add-on or trying to master Java's complex network of classes or JSP tags.
What can you do with PHP?You can do pretty much anything with PHP that you can do with other web-scripting languages and the current version of PHP is fairly robust so its really a matter of taste which of the languages you use. The new version of PHP 4.1 has enhanced features that make PHP an even more robust option for web-programming. The increasing number of large-scale sites using PHP is a great testament to its ability to handle large loads. Take for example such sites as sprint.ca, livebid.amazon.com, sourceforge.net, and mp3.lycos.com and www.neopets.com which all use PHP and have heavy traffic.
In the past PHP ran best in a Unix environment, but the 4.1 version has introduced features such as more robust support to run as an ISAPI plug-in so that it can utilize the multi-threaded features in Windows IIS etc. which reports suggest can make it run as much as 30 times faster than it did in the 4.0.6 version. This makes PHP more enticing than ever as an ASP alternative.
Web CrawlingOne task that we have found PHP is well-suited for is web-crawling. Even though you can perform this task in other laguages we discussed, it takes a few extra lines of code. PHP like Perl treats web files similar to the way it treats local files and has in addition a vast regular expression and string parsing library. The syntax of PHP however we've found is a bit easier to grasp than Perl and is more easily embeddable in html than is Perl. This feature makes it well suited for doing things like web crawling. Compare this to Java where you have to create a URL Connection and then master creating instances of all those string and parser classes or create your own of find customized classes or tags to grab a page etc. or ASP where you have to get an Add-on product or use the XMLDom object.
Below is a snippet of PHP code that gets the content of a web address and then displays it.
GIS MappingOne interesting free opensource product we've found on the web is PHP Mapscript. This is an API that allows PHP to dish out GIS maps (Using an opensource product called Mapserver and Map Script APIs). There are similar Mapscript extensions for other languages such as (Perl, TCL, Python and a Java one in the works).
For more information about these products, please look at the below sites and check out the demos - other Mapping products with similar functionality such as ESRI ArcIMS, MapInfo MapXtreme will cost you thousands of dollars in software and hardware costs and from experience are in many cases slower. Mapserver is suprisingly fast and inexpensive (free except for the time in learning how to use it). The support group is also surprisingly responsive. Post a question to the listserv support group and within 10 minutes, you'll get an answer back to your question.
Querying DatabasesAs stated earlier - PHP comes packaged with numerous fairly robust libraries for querying and updating databases. Best of all - you don't have to pay an arm and a leg to use these libraries.
Unfortunately though - since PHP has these packaged as functions - the function syntax to connect to each different brand of database is slightly different. Compare this to Java which has a generic JDBC interface to connect to databases or ASP which has its ADO abstraction layer or Cold Fusion with its generic CFML query tags .
This is only a minor issue since it is relatively easy to create wrapper classes for these functions so that you only have a single interface to deal with. There are numerous PHP sources on the web that you can get that already do this.
Links of Interest
Back to Papers and Articles