User Guide

Table Of Contents
70 Chapter 3: Using ColdFusion Variables
The Java setVariable Response interface method and C++ CCFX::SetVariable method return
data to the Variables scope of the calling page. Therefore, they are equivalent to setting a Caller
scope variable in a custom ColdFusion tag.
Using scopes as structures
ColdFusion makes all named scopes available as structures. You cannot access the function-local
scope for user defined functions (UDFs) that you define using CFScript as a structure. (In
ColdFusion 4.5 and 5, the following scopes are not available as structures: Variables, Caller,
Client, and Server.)
You can reference the variables in named scopes as elements of a structure. To do so, specify the
scope name as the structure name and the variable name as the key. For example, if you have a
MyVar variable in the Request scope, you can refer to it in either of the following ways:
Request.MyVar
Request["MyVar"]
Similarly, you can use CFML structure functions to manipulate the contents of the scope. For
more information on using structures, see Chapter 5, “Using Arrays and Structures,” on page 97.
Caution: Do not call StructClear(Session) to clear session variables. This deletes the SessionID,
CFID, and CFtoken built-in variables, effectively ending the session. If you want to use StructClear to
delete your application variables, put those variables in a structure in the Session scope, and then
clear that structure. For example, put all your application variables in Session.MyVars and then call
StructClear(Session.MyVars) to clear the variables.
Ensuring variable existence
ColdFusion generates an error if you try to use a variable value that does not exist. Therefore,
before you use any variable whose value is assigned dynamically, you must ensure that a variable
value exists. For example, if your application has a form, it must use some combination of
requiring users to submit data in fields, providing default values for fields, and checking for the
existence of field variable values before they are used.
There are several ways to ensure that a variable exists before you use it, including the following:
You can use the IsDefined function to test for the variables existence.
You can use the cfparam tag to test for a variable and set it to a default value if it does not exist.
You can use a cfform input tag with a hidden attribute to tell ColdFusion to display a helpful
message to any user who does not enter data in a required field. For more information on this
technique, see Chapter 26, “Requiring users to enter values in form fields,” on page 616.
Testing for a variable’s existence
Before relying on a variables existence in an application page, you can test to see if it exists by
using the
IsDefined function. To check whether a specific key exists in a structure, use the
StructKeyExists function.