Home » Tutorials » ColdFusion » Variables

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.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Variables". After Hours Programming. Accessed on March 18, 2024. https://www.afterhoursprogramming.com/tutorial/coldfusion/variables-cf/.

  • Stewart, Suzy. "Variables". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/coldfusion/variables-cf/. Accessed 18 March, 2024.

  • Stewart, Suzy. Variables. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/coldfusion/variables-cf/.



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.