Using periods in variable references

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.

Note:   For more information on scopes, see "About scopes". For more information on references to arrays and structures, see Chapter 5, "Using Arrays and Structures". For more information on references to XML document objects, see Chapter 30, "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 setting the variable value.

Getting a variable

ColdFusion can correctly get variable values even if a simple 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):

  1. Looks for myVar in an internal table of names (the symbol table).
  2. If myVar is the name of a complex object, including a scope, looks for an element named a in the object.

    If myVar is not the name of a complex object, it checks whether myVar.a is the name of a complex object and skips step 3.

  3. If myVar is the name of a complex object, it checks whether a is a complex object,
  4. If a or myVar.a is the name of a complex object, it 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, it checks whether a.b is the name of a simple variable and returns its value.

    If myVar.a is not a complex object, it checks whether myVar.a.B is the name of a simple variable and returns its value.

This way, even if myVar.a.b is a simple variable name, 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 put the simple variable name, in single or double quotation marks, inside the square bracket.

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:

<cfoutput>myVar.a.b is: #myVar.a.b#<br></cfoutput>
<cfoutput>myVar.a.b is: #Variables["myVar.a.b"]#<br></cfoutput>

Setting a variable

ColdFusion cannot be as flexible when it sets a variable value as when it gets a variable, because it must determine the type of variable to create or set. Therefore, the rules for variable names that you set are stricter. Also, the rules vary depending on whether or not the first part of the variable name is the Cookie or Client scope identifier.

For example, assume you have the following code:

<cfset myVar.a.b = "This is a test">

If a variable myVar does not exist, it creates a structure named myVar, creates a structure named a in the structure myVar, creates a key named b in myVar.a, and gives it the value "This is a test". If either myVar or myVar.a exist and is not a structure, ColdFusion generates an error.

In other words, ColdFusion uses the same rules as in the Getting a variable section to resolve the variable name until it finds a name that does not exist yet. It then creates any structures that are needed to create a key named b inside a structure, and assigns the value to the key.

However, if the name before the first period is either Cookie or Client, ColdFusion uses a different rule. It treats all the text. including any periods, that follow the scope name as the name of a simple variable, because Cookie and Client scope variables must be simple. As a result if you have the following code, you see that ColdFusion creates a single, simple Client scope variable named myVar.a.b:

<cfset Client.myVar.a.b = "This is a test">
<cfdump var=#Client.myVar.a.b#>

Creating variables with periods

You should avoid creating the names of simple variables (including arrays) that include periods. However, ColdFusion provides mechanisms for handling cases where you must do so, for example, to maintain compatibility with names of variables in external data sources or to integrate your application with existing code that uses periods in variable names. The following sections describe how to create simple variable names that include periods.

Using brackets to create variables with periods

You can create a variable name that includes periods by using associative array structure notation, as described in "Structure notation," in Chapter 5. To do so, you must do the following:

The following example shows this technique:

<cfset Variables['My.Variable.With.Periods'] = 12>
<cfset Request["Another.Variable.With.Periods"] = "Test variable">
<cfoutput>
  My.Variable.With.Periods is: #My.Variable.With.Periods#<br>
  Request.Another.Variable.With.Periods is:
    #Request.Another.Variable.With.Periods#<br>
</cfoutput>

Creating Client and Cookie variables with periods

To create a Client or Cookie variable with a name that includes one or more periods, simply assign the variable a value. For example, the following line creates a Cookie named User.Preferences.CreditCard:

<cfset Cookie.User.Preferences.CreditCard>

Comments