66
Chapter 3: Using ColdFusion Variables
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:
1
Creates a structure named myVar.
2
Creates a structure named a in the structure myVar.
3
Creates a key named b in myVar.a.
4
Gives it the value "This is a test".
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 114
. To do so, you must do the
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
“About scopes”
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>
Содержание COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...