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.
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.
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.
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.
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.
So, let’s actually create a variable.
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.
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.
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 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.
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.
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:
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.
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.
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 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
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.
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.