ColdFusion uses the period (.) to separate elements of a complex variable such as a structure, query, XML document object, or external object, as in MyStruct.KeyName. A period also separates a variable scope identifier from the variable name, as in Variables.myVariable or CGI.HTTP_COOKIE.
With the exception of Cookie and Client scope variables, which must always be simple variable types, you cannot normally include periods in simple variable names. However, ColdFusion makes some exceptions that accommodate legacy and third-party code that does not conform to this requirement.
For more information, see About scopes, Using Arrays and Structures, and Using XML and WDDX.
Understanding variables and periods
The following descriptions use a sample variable named MyVar.a.b to explain how ColdFusion uses periods when getting and setting the variable value.
Getting a variable
ColdFusion can correctly get variable values even if the variable name includes a period. For example, the following set of steps shows how ColdFusion gets MyVar.a.b, as in <cfset Var2 = myVar.a.b> or IsDefined(myVar.a.b):
- Looks for myVar in an internal table of names (the symbol table).
- If myVar is the name of a complex object, including a scope, ColdFusion looks for an element named "a" in the object. If myVar is not the name of a complex object, checks whether myVar.a is the name of a complex object and skips step 3.
- If myVar is the name of a complex object, checks whether a is a complex object.
- If
a or myVar.a is the name of a complex object, checks whether b is the name of a simple variable, and returns the value of b.If myVar is a complex object but a is not a complex object, checks whether a.b is the name of a simple variable and returns its value.If myVar.a is not a complex object, checks whether myVar.a.b is the name of a simple variable and returns its value.
This way, ColdFusion correctly resolves the variable name and can get its value.
You can also use array notation to get a simple variable with a name that includes periods. In this form of array notation, you use the scope name (or the complex variable that contains the simple variable) as the "array" name. You place the simple variable name, in single- or double-quotation marks, inside the brackets.
Using array notation is more efficient than using plain dot notation because ColdFusion does not have to analyze and look up all the possible key combinations. For example, both of the following lines write the value of myVar.a.b, but the second line is more efficient than the first: