« Removing ".svn" folders from zip archives | Main | How to get dynamic content on static HTML pages »

8 reasons to like CFML's syntax over PHP's

In my work as a web developer, I write both ColdFusion and PHP code for different projects. I much prefer to develop in the CFML language instead of PHP because I find it to be cleaner and simpler. So, I thought I'd write up a few examples and see what the community at large thinks of them. If you've never tried ColdFusion before and what I write is persuasive, I hope that you'll give the free developer version a try. If you disagree, please let me know-- perhaps I'll learn a better way to do things in PHP.

Here are a few examples:

  1. Looping through query output: This is probably my greatest annoyance with outputting data in PHP. In CFML, you can just call CFOUTPUT or CFLOOP and specify which query to loop through, and the variables are automatically accessible to your code. The number of records in the dataset is easily accessible as part of the query object if you need it, but both of the above tags will handle it for you. With PHP, you first have to write a loop for the correct number of rows, which you as the developer have to determine with a database-specific function. Then, for each row, you have to call another database-specific function which can return either indexed- or non-indexed array containing the row's data. What if your rows need to be grouped? In CFML it's easy; just specify a group="[columnName]" attribute in your CFOUTPUT call. You can even code multiple levels of grouping with the same, easy syntax. But with PHP, I'm not even sure how to do this best; I rely on a complicated set of logic where I store the value from the previous row and compare it to the current row to see if it belongs to the same group.

  2. Query caching: CFML has great query caching control, where you can choose the length of the cache time-- or even refresh the cache early if content changes. With PHP, you have to load the memcache library.

  3. Query of queries: I know that PHP doesn't have query-of-queries at all, and what a great feature it is to have. It sure makes queries run quickly and efficiently if you can just query another query already in memory instead of hitting the database every time.

  4. Database transactions: I'm not even sure how to create transactions natively in PHP, my best guess is that you can't specify them-- instead, you would have to write them into your SQL. But with CFML, you don't have to worry about writing in a COMMIT statement into your SQL. Instead, you just wrap the CFTRANSACTION tag around as many queries as you like (of course, the database you're using needs to support transactions for the tag to work).

  5. Writing SQL queries: Since ColdFusion stores database connection information (or reads it from the system DSN settings), you don't have to do all of the steps for the same purpose as you do in PHP. Plus, only CFML offers the cfqueryparam tag, which allows SQL Server to cache its query execution plan.

  6. Making HTTP calls: With CFML, you don't have to worry about whether you have the correct external library installed like you do with PHP; you just write one line with the CFHTTP tag. In PHP, you first have to load the library, configure it with several lines, and then call the URL.

  7. Using lists: I don't know of any PHP functions that make it as easy to handle lists as CFML does. Sure, in PHP you can split or chunk a string into an array. But that takes an extra step and involves translating your original data into something else.

  8. End of session control: In most web application environments, you have no way of knowing whether someone's session has expired (unless they've done you the favor of clicking on your site's "Logout" link). In this circumstance, it's impossible to run code at the end of someone's session because you have no idea which page request is the last. But for ColdFusion, that changed in version 7, which has a method called onSessionEnd. Any code in that method will be executed even without a request from the user. Being able to execute code at the end of a session is fantastic for tracking user behavior and other marketing efforts. So far as I know, this is not available in PHP.

Comments (9)

URL alias says 8 reasons...you think of another after the fact? ;)

Oops! Thanks, Todd. That's fixed. I had meant to remove one item.

While I've never tried Coldfusion, you bring up some interesting points, such as the session close callback, that make me want to look into it. I do think that several of the points you make about php though are due to a lack of utilizing a good php framework that abstracts a lot of what you talk about in #1, #3, #4, and #5. Cake, Symfony, and Code Igniter are a few solid php mvc frameworks.

Anyways, thanks for the info on CF, interesting stuff!

Thanks for your thoughts, Brad. I'll take a look at the frameworks you mentioned and see if they make any of these 8 tasks easier in PHP. (BTW, this is your chance to chance to do the same for Coldfusion! ;) ).

I spent 10(-ish) years writing ColdFusion and recently moved over to a PHP-centric shop. I concur with much of what you write, but there are some good projects out there to mitigate the pain (even without frameworks).

Using a database abstraction layer (like PEAR::MDB2 or php5's PDO) helps with the querying syntax. MDB2 (the only one I've used personally) can also return results as an array which are easier to iterate in PHP than in CF: foreach ( $array as $array_item ) {}. MDB2 also supports transactions (provided the underlying dbms supports them, of course).

HTTP calls haven't been a big deal to me either. PEAR::HTTP handles this pretty nicely with what I consider minimal effort.

I also like the fact that PHP offers ternary operators (I HATE that CF doesn't).

As for everything else...I have to agree. I prefer CF for a lot of reasons, but there are a few pushes and a few things that PHP even does better. Just thought I'd throw that out there.

Just a note on ternary ops, there is at least an IIF() function in CF, although it's a performance hit there just as it can be in some other languages (vs just using , for example).

@Jason, you're right that CF does have a "ternary operator" of a sort in the IIF() function. There are a few instances where I think the use of the function makes code more readable, most often when it lets you pass a conditional argument to a tag or function. Most of the time, however, it can be a pain in the butt to wrap other CF statements inside of quotes and DE()'s. Too bad it can't be implemented as a true operator (e.g. ? : ) instead of as a function.

Wow, I have to commend you for this article. Very clear and well written. Coldfusion is awesome. I struggled with php with some time, met Coldfusion and haven't looked back.

I code both CF and PHP and I am yet to find anything that php does better than CF. The only reason that I have found in favor of PHP is the cheaper servers which pale in comparison to the much shorter development time in CF.

You didn't mention debugging. CF Debugging is awsome. It makes PHP errors look like they came from a kindergartner with a crayon. Debugging should be 1 on the list.

The heart of the problem with PHP which some people would consider an advantage, is that as an open source product, it tries to be all things to all people. I have inherited a lot of PHP sites and there is no consistensy to the methodology. Just the fact that the Server setup and Datasource management are consistant across most Coldfusion apps makes taking over a CF application from an outside source much quicker and easier.

Post a comment


Type the characters you see in the picture above.