|
PHP vs. CFML
PHP is a server platform and a scripting language. The
scripting language contains many useful built-in functions
that enable your pages to display dynamic content. If you
have developed with the PHP scripting language, you'll find
that the ColdFusion language (ColdFusion Markup Language,
or CFML) is quite a bit different, but also similar in many
ways.
First of all, ColdFusion objects (tags) are all inherently
active. In ColdFusion, the tag takes care of the creation
and the cleanup. When the tag is placed on a page, it is
instantiated by the ColdFusion server. The server takes
care of the cleanup as well.
This differs from PHP. When developing in PHP, you must
create an instance of an object, use the object, and then
later destroy the object. For example, when working with
a recordset in PHP, you first create the connection and
the recordset:
<?php
$connMysqlbookstore = mysql_pconnect("tom", "tom", "mypassword") or
die(mysql_error());
mysql_select_db("bookstore", $connMysqlbookstore);
$query_rsGetOrders = "SELECT CustomerID, OrderDate FROM Orders";
$rsGetOrders = mysql_query($query_rsGetOrders, $connMysqlbookstore)
or die(mysql_error());
$row_rsGetOrders = mysql_fetch_assoc($rsGetOrders);
$totalRows_rsGetOrders = mysql_num_rows($rsGetOrders);
?>
Next, you must use the recordset on a page by manually
looping through your recordset:
<table> <?php
do { ?> <tr> <td><?php echo $row_rsGetOrders['CustomerID']; ?></td> <td><?php echo $row_rsGetOrders['OrderDate']; ?></td> </tr> <?php
} while ($row_rsGetOrders = mysql_fetch_assoc($rsGetOrders)); ?> </table>
And finally, if you are done with the recordset, you must
free the results and close the connection:
<?php
mysql_free_result($rsGetOrders);
mysql_close($connMysqlbookstore);
?>
When doing the
same thing in ColdFusion, remember that the ColdFusion tags
are the equivalent of a server-side object in PHP. A <cfquery>
tag is used in place of the recordset in PHP:
<cfquery name= "rsGetOrders" datasource= "bookstore">
SELECT CustomerID, OrderDate FROM Orders
</cfquery>
This code creates the recordset. Once it's created, all
you have to do is display it. Looping is built into the
ColdFusion tags as well, so it's not necessary to manually
start or stop the loop. You can simply output the results
with a <cfoutput query> tag:
<table>
<cfoutput query="rsGetOrders">
<tr>
<td>#rsGetOrders.CustomerID#</td>
<td>#rsGetOrders.OrderDate#</td>
</tr>
</cfoutput>
</table>
The ColdFusion code has several advantages that are evident
right from the start: it's shorter and more concise. It's
also much easier to understand exactly what the code does,
even without programming knowledge. Another advantage is that
cleanup code isn't required—you don't have to destroy
your objects. The server handles those details. Yet another
advantage, that may not be apparent from the code, is that
you can use this recordset again and again on the same page.
You avoid having to reset your pointers as you would in PHP
using a mysql_data_seek() or a similar construct.
Another thing you'll notice from the code is that the database
MySQL is specified. The PHP code will not work with any
other database. You can't simply upgrade your database to
MS SQL Server—because the code will not work. You
can use a database abstraction layer in PHP to simplify
the database communication, but that involves learning another
set of rules and more syntax. With ColdFusion, all databases
are handled the same way by <cfquery>. The connection
to the database is done in the CF Administrator, and the
ColdFusion programmer doesn't have to deal with different
syntax.
Using less code
Less code = less typing = more time for programming
the meat of your program. Now you don't need to spend as
much time on the mundane aspects like the object creation,
keeping track of your loops, and the closing and releasing
of objects. ColdFusion simplifies these tasks across the
board.
Take another example, which demonstrates a common web programming
task: sending an e-mail. The e-mail functionality of PHP
is built-in, as it is in ColdFusion. However, the syntax
is not as intuitive and the code required is more verbose.
Note that in both the ColdFusion and PHP examples, we assume
that the administrator of the application server has set
up the mail server properly:
<cfmail to="#form.tofield#"
from="admin@mywebsite.com"
subject="Your order has been placed">
<cfinclude template="message.cfm">
</cfmail>
If you've ever sent an e-mail from PHP, you know what is
required. First, the mail server must be set up correctly
on the PHP server. Next, you have to know a little bit about
what is required in your mail headers. The PHP mail() function
only takes four parameters—to, subject, message, and
header—but in many cases certain headers have to be
set or the mail will not go through. Some servers will reject
an email if the FROM field is not included. For that reason,
the headers have to be manually concatenated with line feeds.
Knowledge of mail headers is necessary. Alternately, you
can use a pre-written script that you might find on the
web. The same simple e-mail sent from PHP might look like
this:
<?php
$to = $_GET ["txt_tofield"];
// set the to field from an incoming form field
$subject = "Your order confirmation number is: ";
$subject .= $_SESSION_["order_number"];
$message = join("",file("message.txt"));
$from = "From: Admin <admin@mywebsite.com>";
$replyto = "reply-to: admin@mywebsite.com";
$header = $from; // set the from field in the header
$header .= "\n"; // add a line feed
$header .= $replyto; // add the reply-to header to the header
mail($to, $subject, $message, $header); // send the e-mail
?>
In the example above, notice that the body of the message
is included as a separate file (message.txt). The message
body could have easily have been hard-coded into the program,
but with ColdFusion, using include files can be very beneficial.
An include file in ColdFusion is read and interpreted as
though it were part of the page, so it is a simple process
to add a text file to the body of an e-mail message. In
PHP, you have to read the file into an array and then join
the array. Newer versions of PHP will include better file
handling, however.
In ColdFusion, these include files can be conditional.
In other words, you can access different include files based
on criteria that you establish, such as a value in a field
from the database. For example, inside the message.cfm page,
you might have code that looks like this:
<cfswitch expression = #rsGetAllEmails.emailtext#>
<cfcase value="A05">
<cfinclude template="orders_5day.cfm">
</cfcase>
<cfcase value="A15">
<cfinclude template="orders_15day.cfm">
</cfcase>
<cfcase value="A30">
<cfinclude template="orders_30day.cfm">
</cfcase>
<cfcase value="A45">
<cfinclude template="orders_45day.cfm">
</cfcase>
</cfswitch>
This use of conditional includes can be extremely useful
in maintaining your pages. There is no degradation in performance
when using include files in ColdFusion, so you can package
your logic within them.
Built-in functionality
One thing that will become apparent after you've coded
a couple of ColdFusion sites—much of the same functionality
that is built into PHP, or offered as additional modules
that you can utilize, is also built right into the ColdFusion
language using the tag-based syntax. A few of the more powerful
ColdFusion tags (some of them new to ColdFusion MX) are: |