Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Database

PHP & Hosted Applications


Jan01: So Why PHP?

So Why PHP?

There are several options available for server-side web scripting. Here, I limit my discussion to the four that I use on a daily basis — PHP, ASP/VBScript, Perl, and Java Server Pages (JSP). What makes PHP any better or worse than these alternatives? I'll start with JSP.

JSP is Java code intermixed with HTML code. It can execute only on a server that supports JSP and Java servlets. Some web servers do not support Java servlets, and many ISPs or ASPs do not offer it as an option in all configurations. Generally, the development cycle in a Java environment is considerably longer than developing in PHP. The second issue with JSP is that its implementation and future are married to the futures of Java, Java Servlets, and Enterprise JavaBeans. All of these change constantly, mostly for the better. However, the changes are not always backwardly compatible. As any web developer knows, you don't only have to worry about your own code, but also about the technologies that you are using. Using JSP simply means that you have three more layers of software wherein things can go wrong due to no fault of your own code.

The second alternative is Perl, which is a powerful scripting language used not only for web applications, but also for many standalone programs, system administration programs, and software management programs. There's a huge amount of Perl software ready for use. One problem with Perl, however, is that on most web servers, it is executed as an out-of-process CGI program, making it slower and less efficient (PerlScript being the most notable exception).

The major advantage of Perl over PHP is that you can use Perl for various system administration tasks. This may change in the near future since PHP 4 has a superior new scripting engine. The new engine (known as the "Zend engine," see http://www.zend.com/) was designed and implemented in a fashion that makes it easy to use as a standalone scripting environment. Therefore, expect to see it available as a standalone scripting engine, much like Perl, in the near future. Moreover, there are talks between the authors of MySQL and Zend about possibly using the scripting engine in MySQL. If this happens, you will then be able to develop MySQL stored procedures in PHP. Thus, one set of familiar syntax can be used to perform the system administration tasks, business logic at the web server level, and the business logic at the database level.

If you keep your eyes on the URLs when navigating the Web, you will notice that next to HTML, the two most widely used extensions are ASP and PHP. ASP is not really a language, but a framework that lets you run your favorite scripting engine under Microsoft's Internet Information Server (IIS). The most widely used option is VBScript. However, you can easily plug in other engines, such as PerlScript. In this context, when I mention ASP I am referring to running a VBScript engine using the ASP framework under IIS. Both VBScript and PHP are powerful scripting languages that contain almost every feature you would need.

The first glaring difference between the two is the syntax of the language. PHP syntax is based on C/C++, Perl, and some UNIX shell scripting. With a UNIX background, you can pick up the PHP syntax in a few minutes. VBScript is modeled after Visual Basic, which means no real object support, no encapsulation, and no inheritance. Also VBScript lacks many system level functions, for example, most file manipulation functionality that is available under PHP has to be executed via invocation of the external File System Object. Being based on Basic, VBScript has some odd features, such as the While/Wend syntax and the lack of short-circuit Boolean evaluation. For example, if you are reading a record set from a database, and you want to test for a value being NULL or an empty string, then code similar to the following should do the trick in most languages:

if isNull(rs(0)) or Cstr(rs(0)) = "" Then

response.write "Empty Value"

end if

but under VBScript, this code will generate a run-time error if the value is NULL upon invocation of the Cstr() function.

You can still develop well-structured code in VBScript, but not real modular code. It is a chore to split your logic into self-contained functions with any level of OO programming.

Aside from VBScript, one major problem with ASP is that it is IIS centric, which takes UNIX-based servers out of the equation. There are a couple of vendors out there trying to develop UNIX flavors of ASP. The problem with this option is that they will always play catch-up, never being able to release all the new functionality that ASP on IIS offers on the same release date.

Also these options rely heavily on Java. If, for example, you check out one of those options — iASP (that is, instant ASP), the first thing you learn is that you need Java to run it. The first example you will likely see is how to instantiate Java Objects from JAR classes. But if my goal is to work in a Java environment, I will not use ASP but rather JSP or Servlets and EJB.

So what gives PHP the edge? For starters it is available on almost any platform and web server. Language syntax is flexible and robust, yet simple. It is a scripted language, which makes the mechanics of development and testing straightforward, resulting in short development times. PHP is available as open source, and you can learn from it and add your changes. That is, by the way, how PHP 3 and now PHP 4 were developed. While PHP is a free open-source environment, you can still take advantage of commercial support from companies such as Zend.

PHP as a scripted language has no strong typing of variables, thus making it easy for beginners to learn. However, PHP does have basic object support, regular expressions, support for recursion, a complete set of string functions, and encryption functions. It can generate images and PDF files on the fly, connect to a multitude of databases, handle many network protocols, and has almost any functionality required in a scripting language for the Web.

A.A.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.