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.
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.