Introduction

Coldfusion is written right along side your HTML code. ColdFusion is a little different from other server side languages because it is written almost entirely <tags>, just remember that when you are programming. While ColdFusion is very relaxed when it comes to syntax, it is import to maintain a proper variable naming scheme and to have consistency throughout your pages. Well, that was a quick introduction, now let’s’ write our first ColdFusion script! To create your first ever ColdFusion file, give it a name followed by “.cfm” This is the file extension for ColdFusion. What we are going to do here is to write something to the document. If you ever want ColdFusion to output something, you must have the output in the <cfoutput> and </cfoutput> tags.

Example
<html>
    <body>
        <cfoutput>
            ColdFusion Wrote me
        </cfoutput>
    </body>
</html>
Result

ColdFusion Wrote me

As you can see, it is pretty easy. I would recommend breaking your pages into a logic section where you put all of your important code and an output section wrapped with <cfoutput> and ending it with </cfoutput> that way you wouldn’t have to keep putting it around each new thing you want to write to the document. Separating your code into logic and outputting (views) is a great practice. Personally, I recommend them being two different pages and using classes, but we won’t dive into OOP in the ColdFusion tutorial. Most of it is written using a procedural coding practice anyway.The above example was how to output data to the DOM, but ColdFusion has different ways to specify outputting html and to output actual ColdFusion values. You will see how to do this in variables.

Comments

Commenting in ColdFusion is different most other languages. The comment is very much like the HTML comment, but that extra – is super important. Don’t forget it. The ColdFusion comment doesn’t validate in HTML either, so be sure to not to use the ColdFusion comment on strictly HTML pages. I wish circumstances where different and Adobe choose a different commenting style, but that is the hand we are dealt. Always try to comment your work about every 30 lines or so you can remember what the following code does. It’s often a great idea to comment every function. Also, you make it easier for everyone else to figure out what you are doing and why. Like I said try for at least every 30 lines, but comment more when you are dealing with some complex code. One of the best pieces of information I acquired from digging through a previous developers work is that comments are always accurate. Whenever you update a piece of code in ColdFusion, make sure you update the comment too.

Example
<cfoutput>
    <!---ColdFusion Wrote me--->
    No, it didn't
</cfoutput>
Result

No, it didn’t

So, in this example we had <!—ColdFusion Wrote me—> that was a comment, and it is on the server side. This means you will not see the comment at all on the client side. Thank goodness for that because we surely wouldn’t want people to search through our source code and find our deepest commented secrets. Finally, the only text that actually shows up on the client side is the No, it didn’t, which is exactly what we wanted.

Variables

If we ever want to be dynamic, we must start with variables. As a variable means ability to change, what is more dynamic than change? Variables are set in the ColdFusion tag <cfset>. Like I said, ColdFusion isn’t too strict of a language, and you are free to go crazy capitalizing every other letter if you want. However, I definitely would not recommend this practice because you might be in serious danger from anyone that has to read your code. Anyways, ColdFusion still has a few rules for naming variables :

  • Must begin with a letter, underscore, or Unicode currency symbol.
  • Can contain any number of letters, numbers, underscore characters, and Unicode currency symbols.
  • NO SPACES!
  • Not case-sensitive

So, let’s actually create a variable.

Example
<cfoutput>
    <cfset myVar = “Greetings”>
</cfoutput>

The example is pretty simple. Just pick the name, which in our case is myVar. Then, set it equal to some expression or value. An expression can be almost anything from strings to numbers to booleans, etc. By expression, I don’t just mean you have to set it equal to something like 1 or “one”, but that you can set it equal to 1 + 300 / 4 + myOtherVar. In this case, we would be getting the value of another variable and then making the computer solve the expression and assigning it to our variable. Basically, it works just like setting a variable in almost any other language you have ever learned.

Example
<cfset myStringVar = "abc">
<cfset myNumberVar = 123>
<cfset myBooleanVar = true>
<cfoutput>
    #myStringVar#
    <br/>
    #myNumberVar#
    <br/>
    #myBooleanVar#
</cfoutput>
Result

abc
123
true

We just outputted the three most common variable types: string, number, and a boolean. ColdFusion tries its hardest to figure out what type of variable you are trying to make (and ColdFusion is really pretty good at it), but if you can’t figure out why something won’t add, you should try to check the variables first. That pretty much wraps up the simple variables in ColdFusion. We will get into the more complicated ones in some tutorials later.

Operators

Woo! Good choice, math it is. In most programming languages there are at least 5 groups of operators: arithmetic, assignment, increment/decrement, comparison, logical. ColdFusion has all of these operators, but we won’t get to crazy and we will only deal with the first 3.

Arithmetic Operators

Arithmetic operators in ColdFusion are super simple and work just as expected in almost any other language. There is no special integer division or characters that you have to deal with.

Example
<cfset sumVar = 3 + 4>
<cfset differenceVar = 3 - 4>
<cfset productVar = 3 * 4>
<cfset divisionVar = 3 / 4>
<cfset modulusVar = 4 % 3>
<cfoutput> 
    #sumVar# <br/>
    #differenceVar# <br/>
    #productVar# <br/>
    #divisionVar# <br/>
    #modulusVar# <br/>
</cfoutput>
Result

7
-1
12
0.75
1

See? I told you there is nothing special here, they are pretty much standard across most languages.

It is sometimes be useful to join strings. You might want to consider consider just outputting them both right next to each other. Sometimes, you just want to merge to strings into a new variable, but you cannot use the + sign when joining string. However, you can use the & to join strings or strings and numbers.

Example
<cfset myFirstVar = "This is one variable string">
<cfset mySecondVar = " and so is this.">
<cfset myJoinedVar = myFirstVar & mySecondVar >

<cfoutput>
    #myJoinedVar#
</cfoutput>
Result

This is one variable string and so is this.

Assignment Operators

Listen up, I will save you a good amount of time by knowing how to use assignment operators. All assignment operators involve the = mixed with an operator to symbolize that you are using both at the same time. It tells ColdFusion that you want to do whatever the operator is to the following value to the current value. It will be easier to understand with an example:

Example
<cfset assignmentVar = 10>
<cfset assignmentVar += 5>
<cfoutput> 
    #assignmentVar#
</cfoutput>
Result

15

Notice how we didn’t have declare the variable on the right hand side of the statement. This is exactly what the assignment operators eliminate. Obviously, it wouldn’t too helpful if this just applied to addition. You can use all of the arithmetic operators in the exact same fashion as we just used the addition operator. Of course, you don’t have to use them. You could always write assignmentVar = assignmentVar + 5. But, come on. We are coders, and we are proud to be lazy.

Increment and Decrement Operators

Four possible outcomes occur from increments and decrements. Notice the placement of the increment or decrement operator around the variable.

Example
<cfset a = 3>
<cfset b = 3>
<cfset c = 7>
<cfset d = 7>
<cfoutput> 
    #++a#
    #b++#
    #--c#
    #d--#
</cfoutput>
Result

4
3
6
7

If you have ever messed with these guys before, you know what is going on here. If not, just understand that the position of the increment or decrement operator tells us when the action on the variable happens. In the first example, ++a, increments the variable before we print it out. However, in the second example, b++, we don’t increment the variable until after we print it to the screen. It’s a little bit complicated, but it is rather rare to use the incrementing or decrementing before the variable.

If Statements

Don’t flip out on the syntax of if statements in ColdFusion. It is different than most languages. Well, the conditional is different than most languages. It doesn’t use the typical equal sign. Instead, it uses keywords like NEQ, which stands for not equal to. The if tag begins with <cfif> and ends with </cfif> like any normal tag in ColdFusion. We will get to the else ifs and else at the bottom.

Comparison Operators

Comparison operators in ColdFusion are pretty different from most programming languages. Normally, you would see an if statement with a == which means if they thing before the == is equal to the thing after it. ColdFusion has a bit more verbiage about the comparison operators than the others. You might freak out when you see the EQ, NEQ, GT, GTE, LT, and LTE, but don’t. It isn’t as complicated as it looks.

  • EQ – equal to
  • NEQ – not equal to
  • GT – greater than
  • GTE – greater than or equal to
  • LT – less than
  • LTE – less than or equal to
Example
<cfset x = 3>
<cfset y = 4>
<cfoutput>
    <cfif x EQ y>
        If x is equal to y <br/>
    </cfif>
    <cfif x NEQ y>
        If x is not equal to y <br/>
    </cfif>
    <cfif x GT y>
        If x is greater than y <br/>
    </cfif>
    <cfif x GTE y>
        If x is greater than or equal to y <br/>
    </cfif>
    <cfif x LT y>
        If x is less than y <br/>
    </cfif>
    <cfif x LTE y>
        If x is less than or equal to y <br/>
    </cfif>
</cfoutput>

Multiple Conditions

You can add multiple conditions to meet in a conditional. By putting AND between the end of one condition and the beginning of the next condition, you can make sure that both conditions are matched before executing the statements inside the if statement. So, ColdFusion doesn’t use the standard “&&” and will get angry if you try to use it. Also, you can use OR in the exact same manner. Of course, OR would allow the statements to be executed inside of the if statement if any of the conditions around the OR were met. Again, ColdFusion grumbles when you try to use the typical || when you mean OR.

Else if and Else

I almost forget to mention two crucial parts of an if statement. The <cfelseif> and the <cfelse>. The <cfelseif> and <cfelse>, do not actually need closing tags; the next <else> if would actually close the first <cfelseif>, and the <cfelse> would close the <cfelseif> before it.

Example
<cfoutput>
    <cfset x = 5>
    <cfif x EQ 3>
        <p>cfif was triggered.<p>
    <cfelseif x EQ 5> 
        <p>cfelseif was triggered.<p>
    <cfelse>
        <p>cfelse was triggered.<p>
    </cfif>
</cfoutput>

Woo! ColdFusion spit out exactly what we wanted. We had a variable x that was equal to 5. So, we went through the first if <cfif x EQ 3> that basically says if our variable x is equal to 5. It’s not, which moves us on to the next part of the if statement <cfelseif x EQ 5>. Now, the else if here is actually true because our variable x does equal 5. Now, that we have found a match we do not proceed to the <cfelse> because of statement has been satisfied. Other than a few odd syntax changes ColdFusion conditionals are not that difficult.