Which Web scripting platform is right?

Australian tech commentator Daniel Winter looks beyond the hype to show you where and when to use PHP, ASP and ASP.NET for your Web applications.

It is a very frequently asked question and one that tends to rapidly digress into a platform war over the merits of Linux or Windows. This type of argument may actually reflect how difficult it is to perform a side-by-side analysis of competing Web technologies - and this is complicated by the lack of agreement on any comparable operating system baselines.

So rather than adding to this debate, we will attempt to look at the benefits of each technology in terms of its appropriateness to certain Web developing and hosting environments. Although the similarities between ASP and PHP are much closer than a comparison of either to ASP.NET, we will include ASP.NET in the line up as it is often confused with ASP and is intended (by Microsoft) to completely replace it.

The theory - an overview of features

PHP - Hypertext Pre-processor
PHP is an open source server-side scripting language that is very similar in syntax to C languages. Although originally designed to run under Linux using the Apache Web server, it has been ported to work using virtually every operating system and any standards-compliant Web server software. From this it can be derived three of the primary advantages of PHP. Firstly, it is a cross platform technology and consequently PHP applications can be very portable - depending, of course, upon any additional components they are built to incorporate, such as vendor specific databases etc. This portability incurs an additional benefit by virtue of the fact that most Web hosting providers support PHP, making it fairly easy to change hosts if necessary.

Secondly, because PHP bears so much resemblance to C programming languages, it is very easily picked up by developers familiar with this syntax - one that is shared by Java, JavaScript and Perl, amongst others. Thirdly, being open source, PHP is constantly evolving and, more importantly, bug fixes are being regularly implemented to the core libraries, which are freely available.

In addition to these benefits, there are certain programming requirements that may make PHP an appealing choice for developers. Firstly, there are built-in libraries for the direct creation and manipulation of image and PDF documents. This means, for example, that if an application calls for dynamically-created menu images with anti-aliased text, or the exporting of pages to Acrobat format, PHP may be the ideal technology to do it. Although these features are theoretically available to competing technologies, they usually require the installation of third party custom components to do so.

Another situation that may make PHP the best choice of server scripting is where connecting to either mySQL or Postgres databases is required. Although mySQL and Postgres are available to ASP via an ODBC connection, this usually needs to be configured externally by the system administrator. Fortunately, this limitation has been overcome in ASP.NET, where a mySQL data provider is available for direct database connections akin to that used by MS SQL Server.

ASP - Active Server Pages
Microsoft introduced ASP with Windows NT Server 4 as the convention for dynamic Web applications running under their IIS Web server. Because it utilised VBScript, which is itself a variation of the Visual Basic language, ASP was immediately accessible to developers familiar with programming in the Microsoft IDE - Visual Studio. While the scripting language has evolved over time, not much has introduced into ASP to bring it into line with competing technologies. Consequently, there is no integrated support for features like image manipulation as found in PHP. The opportunity is there, however, to extend ASP by writing (or installing) third party COM objects in the form of DLL files. These can be written to perform any action the server itself is capable of doing. The down side, of course, is that this involves interaction with the desktop in order to configure these services - a feature not always available to Web developers.

The upside to ASP is that Microsoft servers are almost ubiquitous in the corporate environment. Additionally, MS SQL Server is also very widely used and, not surprisingly, is well supported in ASP. Although virtually any data source can be made available via ODBC, SQL Server and file DSN access is available at the code level.

ASP.NET
The debate over whether to use ASP or PHP is slowly becoming redundant as .NET builds in momentum. Instead, the predominant argument in years to come will be over whether to use Java or .NET technologies (or both!). The only link between ASP and ASP.NET, really, is that they both use VBScript. Or, in the case of .NET, it can use VBScript - as well as about 20 other languages!

The reason why ASP.NET is in another league to ASP and PHP is that it operates on an entirely different architectural structure. The latter are interpreted scripting languages whereas .NET is a compiled framework. This means, firstly, that Web pages run much, much faster. It also means that source code is safer and more robust. Additionally, ASP.NET introduces a new concept in Web programming - the notion of code-behind pages. With code-behind, each page of HTML is driven by its own compiled programmatical directives. Consequently, the HTML - or presentation layer - is largely separated from the business logic of the application. Although this sort of separation can be achieved in PHP and ASP, it is not an integral part of the technology, as it is in ASP.NET.

Other benefits of ASP.NET are its full-featured integrated support for XML and Web services. There is also a very comprehensive range of security and cryptography libraries available to .NET, making it especially useful for ecommerce and enterprise data applications. On the downside, though, even the experienced programmer can find working with .NET confusing. Irrespective of one's familiarity with the programming language(s) used, the shift in paradigm for the Web developer can be a major stumbling block in ASP.NET. Hosting can also be an issue for ASP.NET applications, as it is not as widely supported by hosting providers as ASP or PHP - and definitely not at competitive rates.

The practice - some language comparisons

Variable Declarations
In VBScript (used by both ASP and ASP.NET), it is not necessary to declare variables before use - although it is recommended practice to do so. Using the Option Explicit declaration, this can be enforced programmatically. In PHP, variables can be declared, although there is no way to enforce this. Instead, they are automatically declared upon use. The one benefit of PHP variables, though, is that they can be set as references to other variables, rather than just by value- as is the case in VBScript.

<% ' VBScript Example
Option Explicit
myVar = 1
myOtherVar = myVar
myVar = 2 ' myResult will be 3
myResult = myVar + myOtherVar
%> <?
// PHP Example
$myVar = 1;
'Use the ampersand to make a reference
$myOtherVar = &$myVar;
$myVar = 2; // $myResult will be 4
$myResult = $myVar + $myOtherVar;
?>

Variable Collections
Working with form and query string variables is very similar in both PHP and ASP. There are ways of accessing the collections of form and query string variables by name or as an array. In ASP.NET, however, things are much different - especially for form fields. Instead of searching blindly for submitted form variables, the code-behind page is made explicitly aware of every form field on the HTML page. The value of these fields can be tested upon the execution of any known event. One such event is the "postback" which occurs when a form is submitted by the user. Other events, however, can be client-side and triggered by JavaScript. In ASP.NET, there is no qualitative distinction between the two.

<%
' ASP Example
myFormVal = request.form("myInputField")
myQSval = request.querystring("myQSitem")
myVal = request.item("myFormOrQSitem")
%>
<?
// PHP 4.1+ Example
$myFormVal = $_POST['myInputField'];
$myQSval = $_REQUEST['myQSitem'];
// PHP 3+ Example
$myFormVal = $HTTP_POST_VARS['myInputField'];
// If register_globals = on
$myVal = $myFormOrQSitem;
?>

<!-- ASP.NET example -->
<html>
<script language="VB" runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Message.Text = "Hello " & Name.Text
End Sub
</script>
<body>
<form action="action.aspx" method="post" runat="server">
Name: <asp:textbox id="Name" runat="server"/>
<asp:button text="OK" OnClick="SubmitBtn_Click"
runat="server"/>
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>

String Concatenation
PHP seems to take the cake here, as it allows variables to be inserted into strings without the usual concatenation issues. ASP.NET makes the whole process much more convoluted with its StringBuilder class - but it runs much faster as a result!

<?
// PHP Example
$link = mysql_connect("host", "user", "password")or die("mysql_error());
mysql_select_db("database") or die("Could not select database");
$query = "SELECT * FROM Table";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
//do something
}
}
?>

Connecting to Databases
When it comes to database connectivity, things are pretty standard for each technology. First of all, in each case a connection to the database is made. In the case of PHP, a database is selected next (this is performed at the connection stage for ASP and ASP.NET). Subsequently a query is constructed and passed to the database which may or may not return a record set.

Because it is more object-oriented in nature, as well as supporting sophisticated error handling, ASP.NET may require significantly more code than either PHP or ASP to perform simple tasks. On the upside, though, far less code is required by ASP.NET for displaying data - especially if the built-in datagrid control is used to automatically construct the HTML output.

<% 'ASP Example
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Driver={SQL Server};Server=MyServerName;" & _
"Database=myDatabaseName;Uid=;Pwd=" const strSQL = "SELECT * FROM Table" Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQL, objConn
Do While Not objRS.EOF
'do something
objRS.MoveNext
Loop
%>

' ASP.NET Example
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim MyConn As SqlConnection = New SqlConnection("server=(local). . . ")
Dim MyComm As SqlCommand = New SqlCommand("select * from Table", MyConn)
MyConn.Open()
Dim dr As SqlDataReader = MyComm.ExecuteReader()
MyDataGrid.DataSource = dr
MyDataGrid.DataBind()
MyConn.Close()
End Sub
</script>
<body>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="600"
BackColor="#FFFFFF"
BorderColor="#000000"
ShowFooter="false"
CellPadding=2
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#EEEEEE"
EnableViewState="false"
/>
</body>
</html>

Conclusion
The choice of ASP, PHP or ASP.NET will ultimately come down to the requirements of the application, as well as the environment where it is to be hosted. The developer's familiarity with similar programming languages and/or paradigms may also help sway the decision one way or the other. The bottom line is that there is no perfect technology and the individual circumstances will dictate the best option. For example, building a one-page form-mailer application for a Windows server in ASP.NET might be considered overkill, but it's a perfect situation for an ASP page. If a site has to work with a mySQL database on a Linux Apache server, though, you'll be fighting a losing battle to use either ASP or ASP.NET. Half the battle between competing technologies will be won if the individual needs are well ironed-out beforehand.