CFML Variables and Scopes
Introduction to Coldfusion Variables
Coldfusion variables are easy to use. Possibly easier than other languages because they are typeless. That is, a variable is never bound to a specific data type. (integer, string, date, etc..) They are also not case sensitive. A variable named myvar is the same as a variable named MyVar. Although not case sensitive, you should be consistent with your variable naming. The general method use is a combination of lower and upper case called bicapitalization but we all usually call it camelcase. It is often thought to be easier to read variable names this way. An example would be thisIsMyVaribleName as opposed to thisismyvariablename.
Coldfusion variables have a few rules for naming. The variable name must begin with a letter or underscore. Not a number or other character. Reserved words are listed in the CFML Reference.
Setting and Reading Variables
So let's set a simple variable:
<cfset variables.myVar = "This is my Var">
We can see the results of this by outputing the variable using cfoutput:
<cfoutput>#variables.myVar#</cfoutput>
The value of a new variable does not have to be text. It can be a number, a date, or even another variable. Variables can also be more complex object such as structure, arrays and queries. That's another tutorial though. Let's set one variable using another and see what happens.
<cfset variables.partA = "This Is ">
<cfset variables.partB = "my Var">
<cfset variables.bothParts = variables.partA & variables.PartB>
<cfoutput>#variables.bothParts#</cfoutput>
To concatenate variables on after the other we use the ampersand '&' as seen above. Some people will do the same as above but like this:
<cfset variables.bothParts = "#variables.partA##variables.PartB#">
This is not recommended - there is simply no reason to do this. It also takes longer to type, and I think it is harder to read. Does it work? Sure. Doesn't mean it's the best thing to do though.
You can mix text strings and variables together also take our example of partA and partB and update it so that it looks like this:
<cfset variables.bothParts = variables.partA & variables.PartB & " and I like it!">
In the introduction I mentioned that our CF variables are typeless. Did you notice how we never told CF that we wanted these variables to be strings? It simple knows that by the value of the variable. Some developers experience in other languages might find this odd after always specifying data types. It works rather smoothly, but it might require that a programmer change some of their coding habits to get used to this.
Dynamically Naming Variables
Sometimes we may need to create new variable names on the go based on the value of other variables. This is generally rare, and beginners will probably never see a reason for this, but it's good to know what you can do, before you have to do it. Also notice that quotes are not required for variable values that are numbers, or other variable names. Many people would do this as <cfset "variables.#var1#" = 123>, which works, sure, but is not following best practices creates harder to read code. The bracket notation is a way to set and read values of structures and arrays. The VARIABLES scope (and the other scopes) is a structure and can be treated as a such when needed.
<cfset variables.var1 = "abc">
<cfset variables[var1] = 123>
<cfoutput>#variables.abc#</cfoutput>
Variable Scopes
All Coldfusion variables belong to a scope. A scope is a specific group of variables for a specific purpose accessibly by specify scripts.
The VARIABLES scope is local to the current script and all scripts included.
The FORM scope only exists when a form is send using method="POST".
The URL scope contains url query string variables that might exist. In index.cfm?id=1 the variable URL.id exists with a value of 1.
The REQUEST scope is available to all scripts running during that page request.
Other scopes are:
Variables
Form
Cookie
Client
Session
Application
Arguments
Attributes
There are even more variable scopes that only exist as the result of a CF tag such as cffile, and cfdirectory. Scoped variables allow a greater separation of different functionality. FORM.username is a different variable than REQUEST.username and can have different values.
It is best practice to ALWAYS scope your variables. If you have variable called myVar is it a form variable? Url? local? Calling it as variables.myVar will remove any doubt, and Coldfusion will not have to search for it in the other scopes.
Persistent Scopes
While most scopes only exist during the execution of the current script, some scopes persist even after that to be used again at another time.
The SESSION scope exists for each user as long as their session is alive. Session timeouts can be set in the CF Administrator and with the cfapplication tag. The variables and value in the SESSION scope can be different for each visitor. This makes it possible to personalize the user experience similar to cookies. The session scope has some huge advantages over cookies. You can store complex data types in sessions. Cookies can not do this. Also cookie information is saved to the hard drive of the client. Session data is stored in the servers RAM, with only session identification stored in a cookie or in the URL.
While the session scope is unique to each visitor, the application scope is shared among the entire application. This makes it great for saving complex variables as a way to cache them for efficient re-use. Both application and session scopes are available to all scripts executed during the page request.
The more you use Coldfusion, you will see how powerfull scopes can be, and how they will enable you to create extremely efficient and dynamic code.
____________