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

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

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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

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