Why PHP?

1.6k words, 10 mins
Introduction

The World Wide Web is a global network of computers, most of them running some form of HTTP (Web) servers. A Web server is a very simple animal, when asked for a web page, it looks for the page and, if it can find it, returns it to the browser. What makes Web servers interesting is when we can ask the server to process the page before returning it. This is the job of server side scripting modules, of which there is wide choice, such as PHP, ASP, Cold Fusion, JSP and Perl CGI. They all have their strengths and their weaknesses and there are situations where one is to be preferred over another.

According to NetCraft surveys, PHP is now the most popular module for the Apache server and in total running on something like 2 million web sites. In this article I will give some of the reasons why PHP is such a popular server side scripting language.

First, let’s examine what happens when a browser requests a page from a Website. We type the URL into the browsers location bar and when we hit enter the browser looks up the address for the server and sends off a request for that page. The web server receives the request, looks on the harddisk for the page, and returns it to the client. In this process, the web server will look at the extension of the filename and if there is a handler registered for that extension, will activate that handler and pass it the page. The handler will load in the page, including any of embedded script or tags, and perform whatever processing is requested and return the results to the web server which then returns it to the client. In these situations the handler could be one of the scripting engines. Note that whatever scripting engine is used the result returned to the client will be pure HTML, there will be no trace of the script itself.

A short history

PHP (which originally stood for Personal Home Page) was first written by Rasmus Lerdorf as a simple set of Perl scripts to track users of his Web pages. He soon had enquiries from other people and rewrote it as a scripting engine. And added support for forms, thus forming PHP/FI. As its popularity grew, a core group of developers created an API for it and turned it into PHP3. By now PHP was being used in situations which were never envisaged, and so a complete rewrite of the script parser was undertaken (the Zend engine), creating PHP4, which was much faster than PHP3. PHP now stood for PHP Hypertext Processor, and was ready for the big time…

What to look for in a scripting engine.

The Four S’s: Speed, Stability, Security and Simplicity.

Speed – not only the speed of execution, which is important, but also that it not slow down the rest of the machine. So it should not demand a lot of system resources. PHP integrates well with other software, especially under UNIX’s, has a small footprint and when run as an Apache module is already loaded for use. Plus, PHP is a thin wrapper around many operating system calls, so can be very fast.

Stability – its no good being fast if the system crashes every few thousand pages. No application is bug free, but having a community of PHP developers and users makes it much harder for bugs to survive for long. Under the hood, PHP uses its own resource management system, and has a sophisticated method for handling variables, making it intrinsically a robust system.

Security – the system should be protected from malicious attacks from users, both as programmers and as surfers. PHP provides many levels of security which can be set in the .ini file to the desired level.

Simplicity – programmers should be able to start being productive as soon as possible. With PHP, even HTML coders can start integrating PHP into their pages straight away. Programmers with previous experience of C, or even with Javascript can get up to speed very quickly.

Oh, and I should mention: Connectability (which doesn’t start with an ‘S’). Because of PHP’s modular system of extensions it will interface with many diverse libraries, and adding further extensions is very simple. This allows PHP to make use of specialised working libraries from many different areas, such as encryption, graphics, XML and so on.

And, some further advantages of PHP.

PHP will run on (almost) any platform. Using the same code base, PHP can be compiled and built on about 25 platforms, including most UNIX’s, Windows(95/98/NT/2000) and Mac’s. As this uses the same code base, all scripts will run identically, whatever the platform.

PHP is similar to C. So anyone who has experience with a C-style language will soon understand PHP. By C-style languages, we can also include Javascript and Java.

In fact, much of PHP’s functionality is provided by wrappers around the underlying system calls (such as fread() and strlen()) so C programmers will immediately feel at home.

PHP is extendible. PHP consists of the core parsing engine (written by Zend), a set of core code modules and then a set of code extensions. This allows programmers two ways of extending PHP to do some special processing, either by writing an extension module and compiling it into the executable, or by creating an executable that can be loaded using PHP’s dynamic loading mechanism.

Lots of HTTP server interfaces. PHP currently will load into Apache, IIS, AOLServer, Roxen and THTTPD. Alternatively, it can be run as a CGI module.

Lots of database interfaces. PHP currently will work with MySQL, MS SQL, Oracle, Informix, PostgreSQL and many others. These are binary level interfaces, and ODBC is also provided for those situations where the database is not supported.

And lots of other modules… when a PHP user wants to interface to particular library, then it is easy to write an interface for it, and many have done so, and contributed it into the main PHP source repository. So one can find modules for graphics routines, PDF files, Flash movies, Cybercash, calendars, XML, IMAP, POP and a host of others. If the library you need is not supported, you can either write one yourself, or employ your favourite programmer to do it.

PEAR. The PHP Extension and Add-on Repository. Similar to the CPAN network for Perl, although still in its infancy, the idea of PEAR is to provide a set of PHP scripts that would be installed by default with the PHP installation

Fast. PHP is normally used an Apache module and this makes it very fast. It is entirely written in C and is quite small, so loads and executes quickly with small memory footprint.

PHP is Open Source. Almost a religious matter to some people! In purely practical terms, it means that you are not dependent on a manufacturer to fix things that don’t work, nor are you forced to pay for upgrades every year to get a working version. Those of us who have waited for Allaire to get something fixed will appreciate this.

On the down side

Error handling is not as sophisticated as Cold Fusion or ASP.

There is no IDE or debugger. An IDE is probably not important for most people and a debugger is promised from Zend Tech. in the very near future. Most people continue using the same environment they use for HTML development.

So when should you use PHP, and when should you use another scripting language?

If you are running on a UNIX or Linux platform, you have the choice between Perl and PHP, both excellent for scripting. Where PHP has the edge is in being much simpler for people new to writing scripts, making it more productive in the earlier stages. Perl also relies on having the correct modules downloaded from CPAN for some of the more off-beat functionality, whereas they come as part of the PHP distribution. Of course, there’s no reason not have both Perl and PHP!

On a Windows platform, there is competition from ASP and Cold Fusion. Here the choice can be as much political as technical, and ASP with IIS is probably a safer bet than PHP with IIS. But on a purely technical basis, PHP is as stable on WindowsNT as either of the other two, at least as fast and less resource intensive. However, ASP offers far better integration into the underlying operating system and databases, especially if you want to make use of COM.

Conclusion

Like so many things in the programmers tool kit, PHP has developed almost accidentally. What started as one person’s experiment with scripting, managed to fulfil a need for many other programmers, a need for a small, fast and simple scripting language that could work seamlessly with many other components. In the last three years PHP has grown to become a major player in the server side scripting market.

Recently, another acronym was coined, LAMP, standing for Linux, Apache, MySQL, PHP, now considered an important package in the Internet world, a set of programs that work together to produce dynamic, data-driven Web sites.

A list of high profile sites can be found at [php.net](http://www .php.net/sites.php), among them are sites that cover the whole range of the Net, from computer magazines to government, finance to games.

Resources

www.php.net
– PHP.net, the central PHP site.

www.zend.com
– Zend, home of the PHP parsing engine. Plus many interesting tutorials and articles.

www.phpbuilder.com
– PHPBuilder, a PHP resource, with articles and tutorials.

px.sklar.com
– the PHP code exchange containing snippets of PHP code.