Having just come from developing PHP for around a year, switching to ColdFusion was an interesting experience.
Being a bit anal about everything seems to help with PHP, but this is true of most languages. For example closing every tag you’ve opened, validating every form to correct user input and handle any mistakes, or explicitly stating every behaviour a page should have.
I have heard it said that anything you can do with ColdFusion you can do with PHP, and vice-versa. However, I’ve found that although it takes some getting used to, there are some distinct advantages to CFML.
How often have PHP developers created a form that takes values, then creates an update or insert query to the database? I recall this to be not entirely painful, but very verbose. First of all, you would either have to write an include file to hold the db connection information, or simply type it out at the top of the script:
mysql_connect(host,username,password)
or die("Error connecting to Database!" . mysql_error());
mysql_select_db(database)
or die("Cannot select database!" . mysql_error());
In ColdFusion this is unnecessary. The connection information is specified server-side as something called a datasource, so it requires 0 lines of code until the query itself is made.
This brings us to the query step.
$query="INSERT INTO users (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
mysql_real_escape_string($_POST(firstname));
mysql_real_escape_string($_POST(lastname));
mysql_real_escape_string($_POST(age));
$result = mysql_query($query);
Technically the mysql_real_escape_string should be used to prevent anyone from inputting anything to intentionally (or unintentionally) corrupt your database, and is standard practise for sanitizing database inputs. In ColdFusion, however, queries are much easier.
<cfquery name=”query” datasource=”database”>
INSERT INTO Persons (FirstName, LastName, Age) VALUES (#firstname#,#lastname#,#age#)
</cfquery>
However, this gets even easier.
It’s pretty safe to assume that what gets put into the database at any point generally comes from some kind of form. Well the form basically spits out the same thing every time, even if the value is blank. So say someone fills out a form containing the firstname, lastname, and age as above and submits it. ColdFusion could handle it all and put it in the database with this:
<cfinsert datasource="database" tablename="users">
What that single line of code does is amazing. It takes the values passed from the form (IMPORTANT: This assumes that the form variable names passed forward match the names of the columns in the database, which is best practise anyway), and automatically creates a query to insert these new values into the database. The same is true for the <cfupdate> tag. Not only that, but ColdFusion has an automated character escape feature, so it always assumes you want special characters escaped when performing a query.
ColdFusion skips all the boring parts of working with databases, and lets you just do what you need to.