Creating Variables With Periods - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

For example, assume you have the following code:
<cfset myVar.a.b = "This is a test">
If a variable myVar does not exist, it does the following:
Creates a structure named myVar.
1
Creates a structure named a in the structure myVar.
2
Creates a key named b in myVar.a.
3
Gives it the value "This is a test".
4
If either myVar or myVar.a exist and neither one is a structure, ColdFusion generates an error.
In other words, ColdFusion uses the same rules as for getting a variable 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. 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
Chapter 5, "Structure notation," on page
following:
Refer to the variable as part of a structure. You can always do this, because ColdFusion
considers all scopes to be structures. For more information on scopes, see
on page
72.
Put the variable name that must include a period inside square brackets and single- or double-
quotation marks.
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>
66
Chapter 3: Using ColdFusion Variables
114. To do so, you must do the
"About scopes"

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Coldfusion mx

Table of Contents