PHP vs CFML Code Comparison Study: web server directory browsing

Fri, May 03, 2013 at 3:00PM

Adam Cameron's ColdFusion blog had a post on his learning PHP and he wrote some CFML which was a bit verbose.  I wanted to show that CFML is almost always less verbose then the equivalent PHP.  Adam's post:

I thought his CFML version was overly complex because it relied on Java objects, slow reflection and manually looped the query instead of using cfloop. I didn't want CFML to look bad, so I rewrote the examples to be as simple as possible.  Adam's solution wasn't wrong or broken, I just wanted to see what the simplest version looks like in both languages.

I noticed he had redefined <cfheader> as a User Defined Function (UDF) because Adobe ColdFusion (ACF) doesn't allow you to use it in cfscript like Railo does.  I didn't realize you can create a function with the same name as a built in tag. I should have done that in my application since it is easier to remember, nice tip there. It seems Coldfusion has no way to list all built-in tags, but Railo does: getTagList(); Someone could make a script that generates wrapper functions for the entire set of coldfusion tags.  Though this doesn't work as well when you have nested tags, so coldfusion needs a UDF for that, whereas Railo just works without making UDFs, and this makes Railo faster too.

I rewrote the CFML version to have the same features as Adam's PHP example and it is still secure (i.e. can't go below web server root path). The CFML rewrite was 20 lines instead of the original 52 lines and I eliminated all the java code.  This made it easier to understand.

The PHP version was 47 lines of code, and I rewrote my simpler CFML version to PHP and it was 27 lines with security in place.

The CFML version is a bit more efficient on disk access since it doesn't have to hit the filesystem again to determine if each record in the directory is a file or directory. Some file/directory names might need to be URLencodedformat() in cfml or urlencode in php, I didn't do that yet.  Edit: These code examples also only work on Linux.

CFML Rewrite

PHP Rewrite

CFML Cfscript Rewrite For Railo

It's interesting that Railo's ability to use tags in cfscript is no more verbose then the tag version. A great advantage over ACF. I personally think the tag version reads cleaner on this example.

The main point here is that CFML does more with less typing and it gets even better as you have more UDFs.  This has always been a selling point of ColdFusion since the early days, and it is still true today after over a decade of enhancements to CFML and other languages like PHP.

Bookmark & Share