PHP vs ColdFusion/Printable version
This is the print version of PHP vs ColdFusion You won't see this message or any elements not part of the book's content when you print or preview this page. |
The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/PHP_vs_ColdFusion
Introduction
When it comes to web design, many languages exist to create the structure for web-based applications. We will be comparing the languages PHP and ColdFusion.
PHP and ColdFusion are both created to help design web pages scripted in HTML. Alone, HTML cannot create dynamic content, connect to databases to perform queries, manipulate files or handle form data. This is where PHP and ColdFusion come in. The code between these languages may be completely different, however both have thousands of different functions that let you do everything from data encryption to image editing. We will be showing code examples between the two.
Hello World
Hello World was seen as early as 1974 in Bell Laboratorie's internal memorandum by Kernighan —Programming in C: A Tutorial— which shows the first known version of the program:
main( ) { printf("Hello, world!"); }
In this tutorial we will use one variable named message to display a string of text.
Putting it Together
editThese files should always be created using a program that doesn't include formating, and saves to a non-rich text format such as notepad.exe (Win32), Pico (Command line), Kedit (KDE), or Gedit (GNOME).
PHP:
<html> <head> <title>Test</title> </head> <body> <?php $message = "Hello World!"; echo $message; ?> </body> </html>
ColdFusion:
<html> <head> <title>Test</title> </head> <body> <cfset message = "Hello World!"> <cfoutput>#message#</cfoutput> </body> </html>
Line Breakdown
editThis is a very simple example because they both do the same thing, in the same amount of code. Not too hard so far eh? Lets see what actually makes them tick...
PHP:
07 <?php 08 $message = "Hello World!"; 09 echo $message; 10 ?>
Line#07 <?php Starts allowing php code to be parsed Line#08 Stores $message with the value Hello World! Line#09 echo Dumps the value Hello World! Line#10 ?> Ends php
ColdFusion:
07 <cfoutput> 08 <cfset message = "Hello World!"> 09 #message# 10 </cfoutput>
Line#07 <cfoutput> Starts the ability to output to the browser Line#08 <cfset Stores #message# with the value Hello World! Line#09 #message# Dumps the value Hello World! Line#10 </cfoutput> End the ability to output to the browser
What the Browser Sees
editEither way, these two scripts produce the same exact page, the only thing different are the server headers.
<html> <head> <title>Test</title> </head> <body> Hello World! </body> </html>
Displayed in Browser
editHello World!
Inserting Variables
Inserting Variables
editBoth languages allow you to insert variables at any point. This is very useful if you have many lines of HTML, but only need to insert something simple. We will use the variable we created above.
PHP:
Message holds: <?=$message; ?>
(Note: This will not work in php if short open tags are disabled. This was enabled by default as of php 4.0.0)
ColdFusion:
Message holds: <cfoutput>#message#</cfoutput>
(Note: It is considered poor form in CFML to place <cfoutput> tags at the beginning and end of a page, as the parser must evaluate every word for variables).
Both examples will print: Message holds: Hello World!
IF Statements
PHP IF STATEMENT
<?php
if( condition ){
// CODE TO EXECUTE
}
?>
The conditions are based on the information you want to compare.
So let's assume you want to compare apples to oranges, to see if they are the same.
if( "apples" == "oranges" ){
// OUTPUT ANSWER
echo "Apples = Oranges. True";
}
Now assume that you want to return an answer even if the conditions are not met. You would use what is called an else statement which looks as follows.
if( "apples" == "oranges" ){
// output
}
else
{
// output alternative
echo "Apples Do Not = Oranges";
}
ColdFusion is very different in its comparisons. They look as follows
<cfif apples eq oranges>
Apples = Oranges
</cfif>
If you notice that it's more of a <> based instead of a structure based. This can be an easier transition for HTML developers as it is tagged based
Now to return an alternate or else you do the almost exact same thing as in PHP, but it looks like this:
<cfif apples eq oranges>
Apples = Oranges
<cfelse>
Apples do not = oranges
</cfif>
Editors
Picking the right editor is essential for web designers. Here we will cover some of the pros and cons of working with certain editors.
Studio 8
editStudio is a software suite from Adobe Systems that includes Dreamweaver, Flash, and Fireworks 8, in addition to Contribute 3 and FlashPaper 2.
Cost: $900 US
Pros
- Easy to Use: Simple, some more complicated functions in Flash, but no coding necessary.
- Standards Compliant: Complies with W3C
Frontpage 2003
editFrontpage is a very neat and handy utility. It offers people to create websites who know zero HTML.
Cost: $199 US
Pros
- Easy to use: Anyone who can use word, can use Frontpage.
- Projects: You can create projects and Frontpage will automatically re-link documents when filesystem changes occur.
Cons
- Doesn't play nice: Frontpage has a tendency to re-write your document to structure code. This usually wipes out PHP code in the document.
- Overly Re-writes: I dont know how many times i've looked at code by Frontpage to only see a plethora of CSS styles. Can get annoying if you are used to a certain way of coding.
- Poor Design: Code is non-W3C complient and hard to use.
- Lack of Support: No longer supported by Microsoft.
Notepad
editNotepad requires you to know pretty much every aspect of HTML, however a wise choice for people who dont like to be told how to code.
Cost: Free
Pros
- Runs fast: No matter what the system (Windows), notepad always performs.
- No Re-writes: Notepad doesn't re-write anything, because it can't.
Cons
- No Projects: No way to manage your documents without external applications or by manually re-writing.
- No Indent Holding: Notepad does not keep your indents, making you tab in over and over.