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:
- 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.
- 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.
- 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.
- 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).
- 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.
- 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.
- 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.
- 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 (11)
December 31, 2007
9:30AM | #
URL alias says 8 reasons...you think of another after the fact? ;)
December 31, 2007
9:46AM | #
Oops! Thanks, Todd. That's fixed. I had meant to remove one item.
January 3, 2008
1:00AM | #
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!
January 3, 2008
10:10AM | #
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! ;) ).
January 9, 2008
1:50PM | #
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.
February 17, 2008
10:07AM | #
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).
February 21, 2008
11:40AM | #
@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.
February 27, 2008
7:14PM | #
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.
August 23, 2008
2:47PM | #
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.
January 20, 2009
8:17AM | #
This mostly applies to CF8, however I have found CFIMAGE and CFEXCHANGE absolute joys to work with; both of these are absolutely possible in PHP, but it is integrated and well tested, as opposed to a dozen modules with inconsistent interfaces among them all.
February 9, 2009
12:59PM | #
1. This is misleading. You're overcomplicating it. You simply write a function. getUsers() or something like that. getUsers returns an array, and can be as little as 3 lines long. I'm not sure what you even mean by "a loop for the correct number of rows". You simply run a "foreach" on the array returned from SQL. Most of the rest of your complaint appears to be that you fail at SQL. You can do some pretty neat things with sorting arrays from query results, by indexing the values to a part of the result. That's probably what you're trying to do there.
2. This is an actual and valid advantage. That being said MySQL's query caching is pretty easy to set up, too, and has the advantage that it clears the cache automatically if the data changes.
3. Misleading. PHP doesn't have a "query" type, so naturally it can't have a type that operates on that type. It can (and does) act extremely well with arrays, though, and searching an array is trivial.
4. Not sure. Don't really use transactions much. :)
5. This is misleading too. CF people seem to think that you have to make a DB connection every time you run a query in PHP, but that's obviously not the case. You merely run the connection as part of a config file. Not that different to CF.
6. That's cURL, an advanced library for complex HTTP requests, that allows you to configure detailed headers and behaviours. For almost all requirements people have for HTTP calls the simple function get_file_contents() is entirely adequate. Or there's file() which reads a file into a line by line array.
7. PHP doesn't support "lists" as a direct thing. A text string is a text string to PHP unless it's broken up, which is, by the way, trivial using $string = explode(',',$string).
8. That's interesting. The last time I used it, CF's sessions were extremely unreliable, but they've probably improved since. I can see it being mildly useful, but hardly critical.