New PHP experiment, inspired by ColdFusion
17th July 2003
I’ve been reading up on ColdFusion MX recently, and I have to admit it looks like a really nice piece of technology. I’d previously written ColdFusion off as being too simplistic and primitive, but having seen how much its capable of I’m reconsidering my position.
If you’ve never seen ColdFusion before, it’s a server side scripting language/application server from Macromedia (who obtained it when they bought Allaire). MX is the latest version of the language, which sees it completely rewritten in Java to allow it to integrate with Java application servers and existing Java servlet applications. This is shrewd marketing on the part of Macromedia, and I’ve already seen them advertising it as something to make your existing Java web deployments easier to customise.
The thing that put me off ColdFusion originally is that it is a tag-based scripting language, specifically designed to make it easy for HTML developers with little or no previous programming experience to pick up. Here’s an example of a chunk of CF syntax:
<cfinclude template="some-other-file.cfm">
<cfif isdefined("form.name")>
<cfoutput>
Hi there, #form.name#
</cfoutput>
</cfif>
The thing that most surprised me about ColdFusion is that although the above syntax looks pretty verbose, it is actually designed in a way that means you can do an awful lot with very little code. The above in PHP would look like this:
<?php
include('some-other-file.php');
if (isset($form['name'])) {
print "Hi there, $form['name']";
}
?>
It’s less typing, but only by a bit. Where CF gets really clever though is with its handling of SQL. Here’s the code to run a SQL query and loop through outputting the results to a simple table:
<cfquery name="users" datasource="myDSN">
select name, email from users order by name
</cfquery>
<table>
<cfoutput query="users">
<tr>
<td>#name#</td><td>#email#</td>
</tr>
</cfoutput>
</table>
Here’s the same in PHP, assuming $db contains a connection to a MySQL database.
<?php
$result = mysql_query("select name, email from users order by name", $db);
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td>$row['name']</td><td>{$row['email']}</td>
</tr>";
}
echo '</table>';
?>
The ColdFusion example achieves the same effect but with less complicated code.
ColdFusion is not without its problems: it comes with a price tag, and it encourages mixing application logic with presentation code (although as with PHP this can be avoided through discipline and careful application design). Never the less, I found it interesting enough that last night I spent a few hours putting together a PHP implementation of a couple of ColdFusion concepts. As with many of the experiments I post here this is very much experimental code—I won’t be supporting it and I would not recommend using it for anything more than casual experimentation. Anyway, here it is:
- Recent Entries (the output of the example script)
- Example script’s source code
- TemplateParser class (does all the work)
- XML Template file used by the example script
With a bit more development, something like this could be a useful tool for quick-and-dirty PHP scripts that simply pull data out of MySQL and display it as HTML. I still prefer PHP, but ColdFusion has a lot of good ideas which are well worth knowing about.
More recent articles
- Qwen2.5-Coder-32B is an LLM that can code well that runs on my Mac - 12th November 2024
- Visualizing local election results with Datasette, Observable and MapLibre GL - 9th November 2024
- Project: VERDAD - tracking misinformation in radio broadcasts using Gemini 1.5 - 7th November 2024