#FirstName# #LastName# is not in Sales,
Accounting, or Administration.
cfloop and cfbreak
The cfloop tag loops through the tag body zero or more times based on a condition specified by the tag attributes.
The cfbreak tag exits a cfloop tag.
cfloop
The cfloop tag provides the following types of loops:
Loop type
Description
Index
Loops through the body of the tag and increments a counter variable by a specified amount after each loop until the
counter reaches a specified value.
Conditional
Checks a condition and runs the body of the tag if the condition is True.
Query
Loops through the body of the tag once for each row in a query.
List, file, or array
Loops through the body of the tag once for each entry in a list, each line in a file, or each item in an array.
Collection
Loops through the body of the tag once for each key in a ColdFusion structure or item in a COM/DCOM object.
The following example shows a simple index loop:
ADOBE COLDFUSION 8 20
ColdFusion Developer’s Guide
The loop index is #LoopCount#.
The following example shows a simple conditional loop. The code does the following:
1
Sets up a ten-element array with the word “kumquats” in the fourth entry.
Loops through the array until it encounters an array element containing “kumquats” or it reaches the end of the
array.
2
3 Prints out the value of the Boolean variable that indicates whether it found the word kumquats and the array
index at which it exited the loop.
i is #i#
foundit is #foundit#
Note: You can get an infinite conditional loop if you do not force an end condition. In this example, the loop is infinite
if you omit the statement. To end an infinite loop, stop the ColdFusion application server.
cfbreak
The cfbreak tag exits the cfloop tag. You typically use it in a cfif tag to exit the loop if a particular condition
occurs. The following example shows the use of a cfbreak tag in a query loop:
You cannot order kumquats! You have ordered #quantity# #fruit#.
cfabort and cfexit
The cfabort tag stops processing of the current page at the location of the cfabort tag. ColdFusion returns to the
user or calling tag everything that was processed before the cfabort tag. You can optionally specify an error message
to display. You can use the cfabort tag as the body of a cfif tag to stop processing a page when a condition, typically
an error, occurs.
The cfexit tag controls the processing of a custom tag, and can only be used in ColdFusion custom tags. For more
information see, “Terminating tag execution” on page 200 and the CFML Reference.
ADOBE COLDFUSION 8 21
ColdFusion Developer’s Guide
Character case
ColdFusion is case-insensitive. For example, the following all represent the cfset tag: cfset, CFSET, CFSet, and
even cfsEt. However, you should get in the habit of consistently using the same case rules in your programs; for
example:
• Develop consistent rules for case use, and stick to them. If you use lowercase characters for some tag names, use
them for all tag names.
• Always use the same case for a variable. For example, do not use both myvariable and MyVariable to represent
the same variable on a page.
Follow these rules to prevent errors on application pages where you use both CFML and case-sensitive languages,
such as JavaScript.
Special characters
The double-quotation marks ("), single-quotation mark ('), and number sign (#) characters have special meaning to
ColdFusion. To include any of them in a string, double the character; for example, use ## to represent a single #
character.
The need to escape the single- and double-quotation marks is context-sensitive. Inside a double-quoted string, you
do not need to escape single-quotation mark (apostrophe) characters. Inside a single-quoted string, you do not
escape double-quotation mark characters.
The following example illustrates escaping special characters, including the use of mixed single- and doublequotation marks:
#mystring#
#mystring2#
Here is a number sign: ##
The output looks like this:
We all said "Happy birthday to you."
Then we said "How old are you now?"
Here is a number sign: #
Reserved words
As with any programming tool, you cannot use just any word or name for ColdFusion variables, UDFs and custom
tags. You must avoid using any name that can be confused with a ColdFusion element. In some cases, if you use a
word that ColdFusion uses—for example, a built-in structure name—you can overwrite the ColdFusion data.
The following list indicates words you must not use for ColdFusion variables, user-defined function names, or
custom tag names. While some of these words can be used safely in some situations, you can prevent errors by
avoiding them entirely. For a complete list of reserved words, see the CFML Reference.
ADOBE COLDFUSION 8 22
ColdFusion Developer’s Guide
•
Built-in function names, such as Now or Hash
•
Scope names, such as Form or Session
• Any name starting with cf. However, when you call a CFML custom tag directly, you prefix the custom tag page
name with cf_.
•
Operators, such as NE or IS
•
The names of any built-in data structures, such as Error or File
•
The names of any built-in variables, such as RecordCount or CGI variable names
•
CFScript language element names such as for, default, or continue
You must also not create form field names ending in any of the following, except to specify a form field validation
rule using a hidden form field name. (For more information on form field validation, see “Introduction to Retrieving
and Formatting Data” on page 511.)
•
_integer
•
_float
•
_range
•
_date
•
_time
•
_eurodate
Because ColdFusion is not case-sensitive, all of the following are reserved words: IS, Is, iS, and is.
CFScript
CFScript is a language within a language. CFScript is a scripting language that is similar to JavaScript but is simpler
to use. Also, unlike JavaScript, CFScript only runs on the ColdFusion server; it does not run on the client system. A
CFScript script can use all ColdFusion functions and all ColdFusion variables that are available in the script’s scope.
CFScript provides a compact and efficient way to write ColdFusion logic. Typical uses of CFScript include:
•
Simplifying and speeding variable setting
•
Building compact flow control structures
•
Encapsulating business logic in user-defined functions
The following sample script populates an array and locates the first array entry that starts with the word “key”. It
shows several of the elements of CFScript, including setting variables, loop structures, script code blocks, and
function calls. Also, the code uses a cfoutput tag to display its results. Although you can use CFScript for output,
the cfoutput tag is usually easier to use.
strings = ArrayNew(1);
strings[1]="the";
strings[2]="key to our";
strings[4]="idea";
for( i=1 ; i LE 4 ; i = i+1 )
{
if(Find("key",strings[i],1))
break; }
ADOBE COLDFUSION 8 23
ColdFusion Developer’s Guide
Entry #i# starts with "key"
You use CFScript to create user-defined functions.
For more information on CFScript, see “Extending ColdFusion Pages with CFML Scripting” on page 92. For more
information on user-defined functions, see “Writing and Calling User-Defined Functions” on page 134.
24
Chapter 3: Using ColdFusion Variables
Adobe ColdFusion variables are the most frequently used operands in ColdFusion expressions. Variable values can
be set and reset, and can be passed as attributes to CFML tags. Variables can be passed as parameters to functions,
and can replace most constants.
To create and use ColdFusion variables, you should know the following:
•
How variables can represent different types of data
•
How the data types get converted
•
How variables exist in different scopes
•
How the scopes are used
•
How to use variables correctly
Contents
Creating variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Variable characteristics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using periods in variable references. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Data type conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
About scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Ensuring variable existence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Validating data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Passing variables to custom tags and UDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Creating variables
You create most ColdFusion variables by assigning them values. (You must use the ArrayNew function to create
arrays.) Most commonly, you create variables by using the cfset tag. You can also use the cfparam tag, and
assignment statements in CFScript. Tags that create data objects also create variables. For example, the cfquery tag
creates a query object variable.
ColdFusion automatically creates some variables that provide information about the results of certain tags or operations. ColdFusion also automatically generates variables in certain scopes, such as Client and Server. For information
on these special variables, see “Reserved Words and Variables” on page 2 in the CFML Reference and the documentation of the CFML tags that create these variables.
ColdFusion generates an error when it tries to use a variable before it is created. This can happen, for example, when
processing data from an incompletely filled form. To prevent such errors, test for the variable’s existence before you
use it. For more information on testing for variable existence, see “Ensuring variable existence” on page 46.
For more information on how to create variables, see “Creating and using variables in scopes” on page 43.
ADOBE COLDFUSION 8 25
ColdFusion Developer’s Guide
Variable naming rules
ColdFusion variable names, including form field names and custom function and ColdFusion component argument
names, must conform to Java naming rules and the following guidelines:
•
A variable name must begin with a letter, underscore, or Unicode currency symbol.
• The initial character can by followed by any number of letters, numbers, underscore characters, and Unicode
currency symbols.
•
A variable name cannot contain spaces.
•
A query result is a type of variable, so it overwrites a local variable with the same name.
•
ColdFusion variables are not case-sensitive. However, consistent capitalization makes the code easier to read.
• When creating a form with fields that are used in a query, match form field names with the corresponding
database field names.
• Periods separate the components of structure or object names. They also separate a variable scope from the
variable name. You cannot use periods in simple variable names, with the exception of variables in the Cookie and
Client scopes. For more information on using periods, see “Using periods in variable references” on page 35.
The following rule applies to variable names, but does not apply to form field and argument names:
1 Prefix each variable’s name with its scope. Although some ColdFusion programmers do not use the Variables
prefix for local variable names, you should use prefixes for all other scopes. Using scope prefixes makes variable
names clearer and increases code efficiency. In many cases, you must prefix the scope. For more information, see
“About scopes” on page 42.
Note: In some cases, when you use an existing variable name, you must enclose it with number signs (#) to allow
ColdFusion to distinguish it from string or HTML text, and to insert its value, as opposed to its name. For more information, see “Using number signs” on page 55.
Variable characteristics
You can classify a variable using the following characteristics:
• The data type of the variable value, which indicates the kind of information a variable represents, such as
number, string, or date
•
The scope of the variable, which indicates where the information is available and how long the variable persists
The following sections provide detailed information on Data types and scopes.
Data types
ColdFusion is often referred to as typeless because you do not assign types to variables and ColdFusion does not
associate a type with the variable name. However, the data that a variable represents does have a type, and the data
type affects how ColdFusion evaluates an expression or function argument. ColdFusion can automatically convert
many data types into others when it evaluates expressions. For simple data, such as numbers and strings, the data
type is unimportant until the variable is used in an expression or as a function argument.
ColdFusion variable data belongs to one of the following type categories:
ADOBE COLDFUSION 8 26
ColdFusion Developer’s Guide
• Simple: One value. Can use directly in ColdFusion expressions. Include numbers, strings, Boolean values, and
date-time values.
• Complex: A container for data. Generally represent more than one value. ColdFusion built-in complex data
types include arrays, structures, queries, and XML document objects.
You cannot use a complex variable, such as an array, directly in a ColdFusion expression, but you can use simple
data type elements of a complex variable in an expression.
For example, with a one-dimensional array of numbers called myArray, you cannot use the expression myArray
* 5. However, you could use an expression myArray[3] * 5 to multiply the third element in the array by five.
•
Binary: Raw data, such as the contents of a GIF file or an executable program file.
• Objects: Complex constructs. Often encapsulate both data and functional operations. The following table lists
the types of objects that ColdFusion can use, and identifies the chapters that describe how to use them:
Object type
See
Component Object Model (COM)
“Integrating COM and CORBA Objects in CFML Applications” on page 972
Common Object Request Broker Architecture (CORBA)
“Integrating COM and CORBA Objects in CFML Applications” on page 972
Java
“Integrating J2EE and Java Elements in CFML Applications” on page 927
ColdFusion component
“Building and Using ColdFusion Components” on page 158
Web service
“Using Web Services” on page 900
Data type notes
Although ColdFusion variables do not have types, it is often convenient to use “variable type” as a shorthand for the
type of data that the variable represents.
ColdFusion can validate the type of data contained in form fields and query parameters. For more information, see
“Testing for a variable’s existence” on page 517 and “Using cfqueryparam” on page 399.
The cfdump tag displays the entire contents of a variable, including ColdFusion complex data structures. It is an
excellent tool for debugging complex data and the code that handles it.
ColdFusion provides the following functions for identifying the data type of a variable:
•
IsArray
•
IsBinary
•
IsBoolean
•
IsObject
•
IsQuery
•
IsSimpleValue
•
IsStruct
•
IsXmlDoc
ColdFusion also includes the following functions for determining whether a string can be represented as or
converted to another data type:
•
IsDate
•
IsNumeric
ADOBE COLDFUSION 8 27
ColdFusion Developer’s Guide
•
IsXML
ColdFusion does not use a null data type. However, if ColdFusion receives a null value from an external source such
as a database, a Java object, or some other mechanism, it maintains the null value until you use it as a simple value.
At that time, ColdFusion converts the null to an empty string (""). Also, you can use the JavaCast function in a call
to a Java object to convert a ColdFusion empty string to a Java null.
Numbers
ColdFusion supports integers and real numbers. You can intermix integers and real numbers in expressions; for
example, 1.2 + 3 evaluates to 4.2.
Integers
ColdFusion supports integers between -2,147,483,648 and 2,147,483,647 (32-bit signed integers). You can assign a
value outside this range to a variable, but ColdFusion initially stores the number as a string. If you use it in an arithmetic expression, ColdFusion converts it into a floating point value, preserving its value, but losing precision as the
following example shows:
mybignum is: #mybignum# mybignumtimes10 is: #mybignumtimes10#
This example generates the following output:
mybignum is: 12345678901234567890
mybignumtimes10 is: 1.23456789012E+020
Real numbers
Real numbers, numbers with a decimal part, are also known as floating point numbers. ColdFusion real numbers
can range from approximately -10300 to approximately 10300. A real number can have up to 12 significant digits. As
with integers, you can assign a variable a value with more digits, but the data is stored as a string. The string is
converted to a real number, and can lose precision, when you use it in an arithmetic expression.
You can represent real numbers in scientific notation. This format is xEy, where x is a positive or negative real
number in the range 1.0 (inclusive) to 10 (exclusive), and y is an integer. The value of a number in scientific notation
is x times 10y. For example, 4.0E2 is 4.0 times 102, which equals 400. Similarly, 2.5E-2 is 2.5 times 10-2, which equals
0.025. Scientific notation is useful for writing very large and very small numbers.
BigDecimal numbers
ColdFusion does not have a special BigDecimal data type for arbitrary length decimal numbers such as
1234567890987564.234678503059281. Instead, it represent such numbers as strings. ColdFusion does, however,
have a PrecisionEvaluate function that can take an arithmetic expression that uses BigDecimal values, calculate
the expression, and return a string with the resulting BigDecimal value. For more information, see
PrecisionEvaluate in the CFML Reference.
Strings
In ColdFusion, text values are stored in strings. You specify strings by enclosing them in either single- or doublequotation marks. For example, the following two strings are equivalent:
ADOBE COLDFUSION 8 28
ColdFusion Developer’s Guide
"This is a string"
'This is a string'
You can write an empty string in the following ways:
•
"" (a pair of double-quotation marks with nothing in between)
•
'' (a pair of single-quotation marks with nothing in between)
Strings can be any length, limited by the amount of available memory on the ColdFusion server. However, the default
size limit for long text retrieval (CLOB) is 64K. The ColdFusion Administrator lets you increase the limit for
database string transfers, but doing so can reduce server performance. To change the limit, select the Enable retrieval
of long text option on the Advanced Settings page for the data source.
Escaping quotation marks and number signs
To include a single-quotation character in a string that is single-quoted, use two single-quotation marks (known as
escaping the single-quotation mark). The following example uses escaped single-quotation marks:
#mystring#
To include a double-quotation mark in a double-quoted string, use two double-quotation marks (known as escaping
the double-quotation mark). The following example uses escaped double-quotation marks:
#mystring#
Because strings can be in either double-quotation marks or single-quotation marks, both of the preceding examples
display the same text:
This is a single-quotation mark: ' This is a double-quotation mark: "
To insert a number sign (#) in a string, you must escape the number sign, as follows:
"This is a number sign ##"
Lists
ColdFusion includes functions that operate on lists, but it does not have a list data type. In ColdFusion, a list is just
a string that consists of multiple entries separated by delimiter characters.
The default delimiter for lists is the comma. If you use any other character to separate list elements, you must specify
the delimiter in the list function. You can also specify multiple delimiter characters. For example, you can tell
ColdFusion to interpret a comma or a semicolon as a delimiter, as the following example shows:
List length using ; and , as delimiters: #listlen(Mylist, ";,")#
List length using only , as a delimiter: #listlen(Mylist)#
This example displays the following output:
List length using ; and , as delimiters: 5
List length using only , as a delimiter: 3
Each delimiter must be a single character. For example, you cannot tell ColdFusion to require two hyphens in a row
as a delimiter.
ADOBE COLDFUSION 8 29
ColdFusion Developer’s Guide
If a list has two delimiters in a row, ColdFusion ignores the empty element. For example, if MyList is "1,2,,3,,4,,,5"
and the delimiter is the comma, the list has five elements and list functions treat it the same as "1,2,3,4,5".
Boolean values
A Boolean value represents whether something is true or false. ColdFusion has two special constants—True and
False—to represent these values. For example, the Boolean expression 1 IS 1 evaluates to True. The expression
"Monkey" CONTAINS "Money" evaluates to False.
You can use Boolean constants directly in expressions, as in the following example:
In Boolean expressions, True, nonzero numbers, and the string “Yes” are equivalent, and False, 0, and the string “No”
are equivalent.
In Boolean expressions, True, nonzero numbers, and the strings “Yes”, “1|”, “True” are equivalent; and False, 0, and
the strings “No”, “0”, and “False” are equivalent.
Boolean evaluation is not case-sensitive. For example, True, TRUE, and true are equivalent.
Date-Time values
ColdFusion can perform operations on date and time values. Date-time values identify a date and time in the range
100 AD to 9999 AD. Although you can specify just a date or a time, ColdFusion uses one data type representation,
called a date-time object, for date, time, and date and time values.
ColdFusion provides many functions to create and manipulate date-time values and to return all or part of the value
in several different formats.
You can enter date and time values directly in a cfset tag with a constant, as follows:
When you do this, ColdFusion stores the information as a string. If you use a date-time function, ColdFusion stores
the value as a date-time object, which is a separate simple data type. When possible, use date-time functions such as
CreateDate and CreateTime to specify dates and times, because these functions can prevent you from specifying
the date or time in an invalid format and they create a date-time object immediately.
Date and time formats
You can directly enter a date, time, or date and time, using standard U.S. date formats. ColdFusion processes the twodigit-year values 0 to 29 as twenty-first century dates; it processes the two-digit-year values 30 to 99 as twentieth
century dates. Time values can include units down to seconds. The following table lists valid date and time formats:
ADOBE COLDFUSION 8 30
ColdFusion Developer’s Guide
To specify
Use these formats
Date
October 30, 2003
Oct 30, 2003
Oct. 30, 2003
10/30/03
2003-10-30
10-30-2003
Time
02:34:12
2:34a
2:34am
02:34am
2am
Date and Time
Any combination of valid date and time formats, such as these:
October 30, 2003 02:34:12
Oct 30, 2003 2:34a
Oct. 30, 2001 2:34am
10/30/03 02:34am
2003-10-30 2am
10-30-2003 2am
Locale-specific dates and times
ColdFusion provides several functions that let you input and output dates and times (and numbers and currency
values) in formats that are specific to the current locale. A locale identifies a language and locality, such as English
(US) or French (Swiss). Use these functions to input or output dates and times in formats other than the U.S.
standard formats. (Use the SetLocale function to specify the locale.) The following example shows how to do this:
#LSDateFormat(Now(), "ddd, dd mmmm, yyyy")#
This example outputs a line like the following:
mar., 03 juin, 2003
For more information on international functions, see “Developing Globalized Applications” on page 336 and the
CFML Reference.
How ColdFusion stores dates and times
ColdFusion stores and manipulates dates and times as date-time objects. Date-time objects store data on a time line
as real numbers. This storage method increases processing efficiency and directly mimics the method used by many
popular database systems. In date-time objects, one day is equal to the difference between two successive integers.
The time portion of the date-and-time value is stored in the fractional part of the real number. The value 0 represents
12:00 AM 12/30/1899.
Although you can use arithmetic operations to manipulate date-and-time values directly, this method can result in
code that is difficult to understand and maintain. Use the ColdFusion date-time manipulation functions instead. For
information on these functions, see the CFML Reference.
ADOBE COLDFUSION 8 31
ColdFusion Developer’s Guide
Binary data type and binary encoding
Binary data (also referred to as a binary object) is raw data, such as the contents of a GIF file or an executable program
file. You do not normally use binary data directly, but you can use the cffile tag to read a binary file into a variable,
typically for conversion to a string binary encoding before transmitting the file using e-mail.
A string binary encoding represents a binary value in a string format that can be transmitted over the web.
ColdFusion supports three binary encoding formats:
Encoding
Format
Base64
Encodes the binary data in the lowest six bits of each byte. It ensures that binary data and non-ANSI character data
can be transmitted using e-mail without corruption. The Base64 algorithm is specified by IETF RFC 2045, at
www.ietf.org/rfc/rfc2045.txt.
Hex
Uses two characters in the range 0-9 and A-F represent the hexadecimal value of each byte; for example, 3A.
UU
Uses the UNIX UUencode algorithm to convert the data.
ColdFusion provides the following functions that convert among string data, binary data, and string encoded binary
data:
Function
Description
BinaryDecode
Converts a string that contains encoded binary data to a binary object.
BinaryEncode
Converts binary data to an encoded string.
CharsetDecode
Converts a string to binary data in a specified character encoding.
CharsetEncode
Converts a binary object to a string in a specified character encoding.
ToBase64
Converts string and binary data to Base64 encoded data.
ToBinary
Converts Base64 encoded data to binary data. The BinaryDecode function provides a superset of the ToBase64
functionality.
ToString
Converts most simple data types to string data. It can convert numbers, date-time objects, and boolean values. (It
converts date-time objects to ODBC timestamp strings.) Adobe recommends that you use the CharsetEncode
function to convert binary data to a string in new applications.
Complex data types
Arrays, structures, and queries are ColdFusion built-in complex data types. Structures and queries are sometimes
referred to as objects, because they are containers for data, not individual data values.
For details on using arrays and structures, see “Using Arrays and Structures” on page 68.
Arrays
Arrays are a way of storing multiple values in a table-like format that can have one or more dimensions. To create an
array and specify its initial dimensions, use the ColdFusion ArrayNew function. For example, the following line
creates an empty two-dimensional array:
You reference elements using numeric indexes, with one index for each dimension. For example, the following line
sets one element of a two-dimensional array to the current date and time:
ADOBE COLDFUSION 8 32
ColdFusion Developer’s Guide
The ArrayNew function can create arrays with up to three dimensions. However, there is no limit on array size or
maximum dimension. To create arrays with more than three dimensions, create arrays of arrays.
After you create an array, you can use functions or direct references to manipulate its contents.
When you assign an existing array to a new variable, ColdFusion creates a new array and copies the old array’s
contents to the new array. The following example creates a copy of the original array:
For more information on using arrays, see “Using Arrays and Structures” on page 68.
Structures
ColdFusion structures consist of key-value pairs, where the keys are text strings and the values can be any ColdFusion
data type, including other structures. Structures let you build a collection of related variables that are grouped under
a single name. To create a structure, use the ColdFusion StructNew function. For example, the following line creates
a new, empty, structure called depts:
You can also create a structure by assigning a value in the structure. For example, the following line creates a new
structure called MyStruct with a key named MyValue, equal to 2:
Note: In previous ColdFusion versions, this line created a Variables scope variable named "MyStruct.MyValue" with the
value 2.
After you create a structure, you can use functions or direct references to manipulate its contents, including adding
key-value pairs.
You can use either of the following methods to reference elements stored in a structure:
• StructureName.KeyName
• StructureName["KeyName"]
The following examples show these methods:
depts.John="Sales"
depts["John"]="Sales"
When you assign an existing structure to a new variable, ColdFusion does not create a new structure. Instead, the
new variable accesses the same data (location) in memory as the original structure variable. In other words, both
variables are references to the same object.
For example, the following line creates a new variable, myStructure2, that is a reference to the same structure as the
myStructure variable:
When you change the contents of myStructure2, you also change the contents of myStructure. To copy the contents
of a structure, use the ColdFusion Duplicate function, which copies the contents of structures and other complex
data types.
Structure key names can be the names of complex data objects, including structures or arrays. This lets you create
arbitrarily complex structures.
For more information on using structures, see “Using Arrays and Structures” on page 68.
ADOBE COLDFUSION 8 33
ColdFusion Developer’s Guide
Queries
A query object, sometimes referred to as a query, query result, or record set, is a complex ColdFusion data type that
represents data in a set of named columns, similar to the columns of a database table. The following ColdFusion tags
can create query objects:
• cfquery
• cfdirectory
• cfhttp
• cfldap
• cfpop
• cfprocresult
In these tags, the name attribute specifies the query object’s variable name. The QueryNew function also creates query
objects.
When you assign a query to a new variable, ColdFusion does not copy the query object. Instead, both names point
to the same record set data. For example, the following line creates a new variable, myQuery2, that references the
same record set as the myQuery variable:
If you make changes to data in myQuery, myQuery2 also shows those changes.
You reference query columns by specifying the query name, a period, and the column name; for example:
myQuery.Dept_ID
When you reference query columns inside tags, such as cfoutput and cfloop, in which you specify the query name
in a tag attribute, you do not have to specify the query name.
You can access query columns as if they are one-dimensional arrays. For example, the following line assigns the
contents of the Employee column in the second row of the myQuery query to the variable myVar:
Note: You cannot use array notation to refer to a row (of all columns) of a query. For example, myQuery[2] does not
refer to the second row of the myQuery query object.
Working with structures and queries
Because structure variables and query variables are references to objects, the rules in the following sections apply to
both types of data.
Multiple references to an object
When multiple variables refer to a structure or query object, the object continues to exist as long as at least one
reference to the object exists. The following example shows how this works:
depts = structnew();
#newStructure.John#
#depts#
This example displays the following output:
Sales
ADOBE COLDFUSION 8 34
ColdFusion Developer’s Guide
0
After the tag executes, the depts variable does not refer to a structure; it is a simple variable with
the value 0. However, the variable newStructure still refers to the original structure object.
Assigning objects to scopes
You can give a query or structure a different scope by assigning it to a new variable in the other scope. For example,
the following line creates a server variable, Server.SScopeQuery, using the local myquery variable:
To clear the server scope query variable, reassign the query object, as follows:
This deletes the reference to the object from the server scope, but does not remove any other references that might
exist.
Copying and duplicating objects
You can use the Duplicate function to make a true copy of a structure or query object. Changes to the copy do not
affect the original.
Using a query column
When you are not inside a cfloop, cfoutput, or cfmail tag that has a query attribute, you can treat a query column
as an array. However, query column references do not always behave as you might expect. This section explains the
behavior of references to query columns using the results of the following cfquery tag in its examples:
SELECT FirstName, LastName
FROM Employee
To reference elements in a query column, use the row number as an array index. For example, both of the following
lines display the word "ben":
#myQuery.Firstname[1]# #myQuery["Firstname"][1]#
ColdFusion behavior is less straightforward, however, when you use the query column references
myQuery.Firstname and myQuery["Firstname"] without using an array index. The two reference formats produce
different results.
If you refer to myQuery.Firstname, ColdFusion automatically converts it to the first row in the column. For example,
the following lines print the word "ben":
#mycol#
But the following lines display an error message:
#mycol[1]#
If you refer to Query["Firstname"], ColdFusion does not automatically convert it to the first row of the column.
For example, the following line results in an error message indicating that ColdFusion cannot convert a complex type
to a simple value:
#myQuery['Firstname']#
Similarly, the following lines print the name "marjorie", the value of the second row in the column:
ADOBE COLDFUSION 8 35
ColdFusion Developer’s Guide
#mycol[2]#
However, when you make an assignment that requires a simple value, ColdFusion automatically converts the query
column to the value of the first row. For example, the following lines display the name "ben" twice:
#myQuery.Firstname# #myVar#
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.
For more information, see “About scopes” on page 42, “Using Arrays and Structures” on page 68, and “Using XML
and WDDX” on page 865.
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 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, 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, checks whether a is a complex object.
4 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 put the simple variable name, in single- or double-quotation marks, inside the square brackets.
ADOBE COLDFUSION 8 36
ColdFusion Developer’s Guide
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:
myVar.a.b is: #myVar.a.b# myVar.a.b is: #Variables["myVar.a.b"]#
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 the first part of the variable name is the Cookie or Client scope identifier.
For example, assume you have the following code:
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:
Creating variables with periods
You should avoid creating the names of variables (except for dot notation in structures) 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” on page 79. 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 42.
•
Put the variable name that must include a period inside square brackets and single- or double-quotation marks.
The following example shows this technique:
ADOBE COLDFUSION 8 37
ColdFusion Developer’s Guide
My.Variable.With.Periods is: #My.Variable.With.Periods#
Request.Another.Variable.With.Periods is:
#Request.Another.Variable.With.Periods#
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:
Data type conversion
ColdFusion automatically converts between data types to satisfy the requirements of an expression’s operations,
including a function’s argument requirements. As a result, you generally don’t need to be concerned about compatibility between data types and the conversions from one data type to another. However, understanding how
ColdFusion evaluates data values and converts data between types can help you prevent errors and create code more
effectively.
Operation-driven evaluation
Conventional programming languages enforce strict rules about mixing objects of different types in expressions. For
example, in a language such as C++ or Basic, the expression ("8" * 10) produces an error because the multiplication operator requires two numerical operands and "8" is a string. When you program in such languages, you must
convert between data types to ensure error-free program execution. For example, the previous expression might have
to be written as (ToNumber("8") * 10).
In ColdFusion, however, the expression ("8" * 10) evaluates to the number 80 without generating an error. When
ColdFusion processes the multiplication operator, it automatically attempts to convert its operands to numbers.
Since "8" can be successfully converted to the number 8, the expression evaluates to 80.
ColdFusion processes expressions and functions in the following sequence:
1 For each operator in an expression, it determines the required operands. (For example, the multiplication
operator requires numeric operands and the CONTAINS operator requires string operands.)
For functions, it determines the type required for each function argument. (For example, the Min function
requires two numbers as arguments and the Len function requires a string.)
2
It evaluates all operands or function arguments.
3 It converts all operands or arguments whose types differ from the required type. If a conversion fails, it reports
an error.
Conversion between types
Although the expression evaluation mechanism in ColdFusion is very powerful, it cannot automatically convert all
data. For example, the expression "eight" * 10 produces an error because ColdFusion cannot convert the string
"eight" to the number 8. Therefore, you must understand the rules for conversion between data types.
ADOBE COLDFUSION 8 38
ColdFusion Developer’s Guide
The following table explains how conversions are performed. The first column shows values to convert. The
remaining columns show the result of conversion to the listed data type.
Value
As Boolean
As number
As date-time
As string
"Yes"
True
1
Error
"Yes"
"No"
False
0
Error
"No"
True
True
1
Error
"Yes"
False
False
0
Error
"No"
Number
True if Number is not 0;
False otherwise.
Number
See “Date-time values” earlier in String representation
this chapter.
of the number (for
example, “8”).
String
If "Yes", True
If it represents a number (for
example, "1,000" or "12.36E-12"), it is
If "No", False
converted to the corresponding
number. If it represents a date-time
If it can be converted to 0,
(see next column), it is converted to
False
the numeric value of the correIf it can be converted to any sponding date-time object.
other number, True
If it is an ODBC date, time, or
String
timestamp (for example "{ts
'2001-06-14 11:30:13'}", or if it is
expressed in a standard U.S.
date or time format, including
the use of full or abbreviated
month names, it is converted to
the corresponding date-time
value.
Days of the week or unusual
punctuation result in an error.
Dashes, forward-slashes, and
spaces are generally allowed.
Date
Error
The numeric value of the date-time
object.
Date
An ODBC timestamp.
ColdFusion cannot convert complex types, such as arrays, queries, and COM objects, to other types. However, it can
convert simple data elements of complex types to other simple data types.
Type conversion considerations
The following sections detail specific rules and considerations for converting between types.
The cfoutput tag
The cfoutput tag always displays data as a string. As a result, when you display a variable using the cfoutput tag,
ColdFusion applies the type conversion rules to any non-string data before displaying it. For example, the cfoutput
tag displays a date-time value as an ODBC timestamp.
Case-insensitivity and Boolean conversion
Because ColdFusion expression evaluation is not case-sensitive, Yes, YES, and yes are equivalent; False, FALSE, and
false are equivalent; No, NO, and no are equivalent; and True, TRUE, and true are equivalent.
Converting binary data
ColdFusion cannot automatically convert binary data to other data types. To convert binary data, use the ToBase64
and ToString functions. For more information, see “Binary data type and binary encoding” on page 31.
Converting date and time data
To ensure that a date and time value is expressed as a real number, add zero to the variable. The following example
shows this:
ADOBE COLDFUSION 8 39
ColdFusion Developer’s Guide
Use cfoutput to display the result of the now function: #mynow#
Now add 0 to the result and display it again: #mynow#
At 1:06 PM on June 6, 2003, its output looked like this:
Use cfoutput to display the result of the now function:
{ts '2003-06-03 13:06:44'}
Now add 0 to the result and display it again:
37775.5463426
Converting numeric values
When ColdFusion evaluates an expression that includes both integers and real numbers, the result is a real number.
To convert a real number to an integer, use a ColdFusion function. The Int, Round, Fix, and Ceiling functions
convert real numbers to integers, and differ in their treatment of the fractional part of the number.
If you use a hidden form field with a name that has the suffix _integer or _range to validate a form input field,
ColdFusion truncates real numbers entered into the field and passes the resulting integer to the action page.
If you use a hidden form field with a name that has the suffix _integer, _float, or _range to validate a form input
field, and the entered data contains a dollar amount (including a dollar sign) or a numeric value with commas,
ColdFusion considers the input to be valid, removes the dollar sign or commas from the value, and passes the
resulting integer or real number to the action page.
ColdFusion does not have an inherent data type for arbitrary precision decimal numbers (BigDecimal numbers).
ColdFusion initially saves such numbers as strings, and if you use them in an expression, converts the value to a
numeric type, often losing precision. You can retain precision by using the PrecisionEvaluate method, which
evaluates string expressions using BigDecimal precision arithmetic and can return the result as a long string of
numbers. For more information, see PrecisionEvaluate in the CFML Reference.
Evaluation and type conversion issues
The following sections explain several issues that you might encounter with type evaluation and conversion.
Comparing variables to True or False
You might expect the following two cfif tag examples to produce the same results:
myVariable equals #myVariable# and is True
myVariable equals #myVariable# and is True
However, if myVariable has a numeric value such as 12, only the first example produces a result. In the second case,
the value of myVariable is not converted to a Boolean data type, because the IS operator does not require a specific
data type and just tests the two values for identity. Therefore, ColdFusion compares the value 12 with the constant
True. The two are not equal, so nothing is printed. If myVariable is 1, "Yes", or True, however, both examples print
the same result, because ColdFusion considers these to be identical to Boolean True.
ADOBE COLDFUSION 8 40
ColdFusion Developer’s Guide
If you use the following code, the output statement does display, because the value of the variable, 12, is not equal to
the Boolean value False:
myVariable equals #myVariable# and IS NOT False
As a result, you should use the test , and not use the IS comparison operator when testing
whether a variable is True or False. This issue is a case of the more general problem of ambiguous type expression
evaluation, described in the following section.
Ambiguous type expressions and strings
When ColdFusion evaluates an expression that does not require strings, including all comparison operations, such
as IS or GT, it checks whether it can convert each string value to a number or date-time object. If so, ColdFusion
converts it to the corresponding number or date-time value (which is stored as a number). It then uses the number
in the expression.
Short strings, such as 1a and 2P, can produce unexpected results. ColdFusion can interpret a single "a" as AM and a
single "P" as PM. This can cause ColdFusion to interpret strings as date-time values in cases where this was not
intended.
Similarly, if the strings can be interpreted as numbers, you might get unexpected results.
For example, ColdFusion interprets the following expressions as shown:
Expression
Interpretation
If 1:00am is 1:00am.
If 1:00pm is later than 2:00am.
Treat the variable age as 4:00 am, convert it to the date-time value 0.16666666667, and add 7 to make it
7.16666666667.
If 0 is 0.
To prevent such ambiguities when you compare strings, use the ColdFusion string comparison functions Compare
and CompareNoCase, instead of the comparison operators.
You can also use the IsDate function to determine whether a string can be interpreted as a date-time value, or to
add characters to a string before comparison to avoid incorrect interpretation.
Date-time functions and queries when ODBC is not supported
Many CFML functions, including the Now, CreateDate, CreateTime, and CreateDateTime functions, return datetime objects. ColdFusion creates Open Database Connectivity (ODBC) timestamp values when it converts date-time
objects to strings. As a result, you might get unexpected results when using dates with a database driver that does not
support ODBC escape sequences, or when you use SQL in a query of queries.
If you use SQL to insert data into a database or in a WHERE clause to select data from a database, and the database
driver does not support ODBC-formatted dates, use the DateFormat function to convert the date-time value to a
valid format for the driver. This rule also applies to queries of queries.
For example, the following SQL statement uses the DateFormat function in a query of queries to select rows that
have MyDate values in the future:
ADOBE COLDFUSION 8 41
ColdFusion Developer’s Guide
SELECT *
FROM DateQuery
WHERE MyDate >= '#DateFormat(Now())#'
The following query of queries fails with the error message “Error: {ts is not a valid date,” because the ColdFusion
Now function returns an ODBC timestamp:
SELECT *
FROM DateQuery
WHERE MyDate >= '#now()#'
Using JavaCast with overloaded Java methods
You can overload Java methods so a class can have several identically named methods that differ only in parameter
data types. At run time, the Java virtual machine attempts to resolve the specific method to use, based on the types
of the parameters passed in the call. Because ColdFusion does not use explicit types, you cannot predict which
version of the method the virtual machine will use.
The ColdFusion JavaCast function helps you ensure that the right method executes by specifying the Java type of
a variable, as in the following example:
The JavaCast function takes two parameters: a string representing the Java data type and the variable whose type
you are setting. You can specify the following Java data types: bool, int, long, float, double, and String.
For more information on the JavaCast function, see the CFML Reference.
Using quotation marks
To ensure that ColdFusion properly interprets string data, surround strings in single- or double-quotation marks.
For example, ColdFusion evaluates “10/2/2001” as a string that can be converted into a date-time object. However,
it evaluates 10/2/2001 as a mathematical expression, 5/2001, which evaluates to 0.00249875062469.
Examples of type conversion in expression evaluation
The following examples demonstrate ColdFusion expression evaluation.
Example 1
2 * True + "YES" - ('y' & "es")
Result value as string: "2"
Explanation: (2*True) is equal to 2; ("YES"- "yes") is equal to 0; 2 + 0 equals 2.
Example 2
"Five is " & 5
Result value as string: "Five is 5"
Explanation: 5 is converted to the string "5".
Example 3
DateFormat("October 30, 2001" + 1)
Result value as string: "31-Oct-01"
ADOBE COLDFUSION 8 42
ColdFusion Developer’s Guide
Explanation: The addition operator forces the string "October 30, 2001" to be converted to a date-time object, and
then to a number. The number is incremented by one. The DateFormat function requires its argument to be a datetime object; thus, the result of the addition is converted to a date-time object. One is added to the date-time object,
moving it ahead by one day to October 31, 2001.
About scopes
Variables differ in how they are set (by your code or by ColdFusion), the places in your code where they are
meaningful, and how long their values persist. These considerations are generally referred to as a variable’s scope.
Commonly used scopes include the Variables scope, the default scope for variables that you create, and the Request
scope, which is available for the duration of an HTTP request.
Note: User-defined functions also belong to scopes. For more information, see “Specifying the scope of a function” on
page 153.
Scope types
The following table describes ColdFusion scopes:
.
Scope
Description
Application
Contains variables that are associated with one, named application on a server. The cfapplication tag name
attribute or the Application.cfc This.name variable setting specifies the application name. For more information, see
“Using Persistent Data and Locking” on page 272.
Arguments
Variables passed in a call to a user-defined function or ColdFusion component method. For more information, see
“About the Arguments scope” on page 142.
Attributes
Used only in custom tag pages and threads. Contains the values passed by the calling page or cfthread tag in the
tag’s attributes. For more information, see “Creating and Using Custom CFML Tags” on page 190 and “Using ColdFusion Threads” on page 300.
Caller
Used only in custom tag pages. The custom tag’s Caller scope is a reference to the calling page’s Variables scope. Any
variables that you create or change in the custom tag page using the Caller scope are visible in the calling page’s Variables scope. For more information, see “Creating and Using Custom CFML Tags” on page 190.
CGI
Contains environment variables identifying the context in which a page was requested. The variables available
depend on the browser and server software. For a list of the commonly used CGI variables, see “Reserved Words and
Variables” on page 2 in the CFML Reference.
Client
Contains variables that are associated with one client. Client variables let you maintain state as a user moves from
page to page in an application, and are available across browser sessions. By default, Client variables are stored in
the system registry, but you can store them in a cookie or a database. Client variables cannot be complex data types
and can include periods in their names. For more information, see “Using Persistent Data and Locking” on page 272.
Cookie
Contains variables maintained in a user’s browser as cookies. Cookies are typically stored in a file on the browser, so
they are available across browser sessions and applications. You can create memory-only Cookie variables, which are
not available after the user closes the browser. Cookie scope variable names can include periods.
Flash
Variables sent by a Flash movie to ColdFusion and returned by ColdFusion to the movie. For more information, see
“Using the Flash Remoting Service” on page 674.
Form
Contains variables passed from a Form page to its action page as the result of submitting the form. (If you use the
HTML form tag, you must use method="post".) For more information, see “Introduction to Retrieving and Formatting Data” on page 511.
function local
Contains variables that are declared inside a user-defined function or ColdFusion component method and exist only
while a function executes. For more information, see “Writing and Calling User-Defined Functions” on page 134.
ADOBE COLDFUSION 8 43
ColdFusion Developer’s Guide
Scope
Description
Request
Used to hold data that must be available for the duration of one HTTP request. The Request scope is available to all
pages, including custom tags and nested custom tags, that are processed in response to the request.
This scope is useful for nested (child/parent) tags. This scope can often be used in place of the Application scope, to
avoid the need for locking variables. Several chapters discuss using the Request scope.
Server
Contains variables that are associated with the current ColdFusion server. This scope lets you define variables that
are available to all your ColdFusion pages, across multiple applications. For more information, see “Using Persistent
Data and Locking” on page 272.
Session
Contains variables that are associated with one client and persist only as long as the client maintains a session. They
are stored in the server’s memory and can be set to time out after a period of inactivity. For more information, see
“Using Persistent Data and Locking” on page 272.
This
Exists only in ColdFusion components or cffunction tags that are part of a containing object such as a ColdFusion
Struct. Exists for the duration of the component instance or containing object. Data in the This scope is accessible
from outside the component or container by using the instance or object name as a prefix.
ThisTag
Used only in custom tag pages. The ThisTag scope is active for the current invocation of the tag. If a custom tag
contains a nested tag, any ThisTag scope values you set before calling the nested tag are preserved when the nested
tag returns to the calling tag.
The ThisTag scope includes three built-in variables that identify the tag’s execution mode, contain the tag’s generated contents, and indicate whether the tag has an end tag.
A nested custom tag can use the cfassociate tag to return values to the calling tag’s ThisTag scope. For more information, see “Accessing tag instance data” on page 198.
Thread
Variables that are created and changed inside a ColdFusion thread, but can be read by all code on the page that
creates the thread. Each thread has a Thread scope that is a subscope of a cfthread scope. For more information, see
“Using ColdFusion Threads” on page 300.
thread local
Variables that are available only within a ColdFusion thread. For more information, see “Using ColdFusion Threads”
on page 300.
URL
Contains parameters passed to the current page in the URL that is used to call it. The parameters are appended to
the URL in the format ?variablename = value[&variablename=value...]; for example www.MyCompany.com/inputpage.cfm?productCode=A12CD1510&quantity=3.
Note: If a URL includes multiple parameters with the same name, the resulting variable in the ColdFusion URL scope
consists of all parameter values separated by commas. For example, a URL of the form http://localhost/urlparamtest.cfm? param=1¶m=2¶m=3 results in a URL.param variable value of 1,2,3 on the ColdFusion page.
Variables (local)
The default scope for variables of any type that are created with the cfset and cfparam tags. A local variable is
available only on the page on which it is created and any included pages (see also the Caller scope).
Important: To prevent data corruption, you lock code that uses Session, Application, or Server scope variables. For more
information, see “Using Persistent Data and Locking” on page 272.
Creating and using variables in scopes
The following table shows how you create and refer to variables in different scopes in your code. For more information on the mechanisms for creating variables in most scopes, see “Creating variables” on page 24.
ADOBE COLDFUSION 8 44
ColdFusion Developer’s Guide
Scope prefix
Prefix required to
reference
Where available
Created by
(function
local, no
prefix)
Prohibited
Within the body of a user-defined function or
ColdFusion component method, only while the
function executes.
In the function or method definition, a var
keyword in a cfset tag or a CFScript var statement.
Application
Yes
For multiple clients in one application over
multiple browser sessions. Surround code that
uses application variables in cflock blocks.
Specifying the prefix Application when you
create the variable.
Arguments
No
Within the body of a user-defined function or
ColdFusion component method.
The calling page passing an argument in the
function call.
Attributes
Yes
On a custom tag page, or inside a thread
For custom tags, the calling page passing the
values to a custom tag page in the custom tag’s
attributes.
(type)
For threads, the cfthread tag specifying
attribute values.
Caller
On the custom tag
page, Yes.
On the custom tag page, by using the Caller
scope prefix.
On the custom tag page, by specifying the prefix
Caller when you create the variable.
On the calling page,
No (Variables prefix
is optional).
On the page that calls the custom tag, as local
variables (Variables scope).
On the calling page, by specifying the prefix Variables, or using no prefix, when you create the
variable.
Cffile
Yes
Following an invocation of cffile.
A cffile tag.
CGI
No
On any page. Values are specific to the latest
browser request.
The web server. Contains the server environment
variables that result from the browser request.
Client
No
For one client in one application, over multiple
browser sessions.
Specifying the prefix Client when you create the
variable.
Cookie
No
For one client in one or more applications and
pages, over multiple browser sessions.
A cfcookie tag. You can also set memory-only
cookies by specifying the prefix Cookie when
you create the variable.
Flash
Yes
A ColdFusion page or ColdFusion component
called by a Flash client.
The ColdFusion Client access. You assign a value
to Flash.You can assign values to the Flash.result
and Flash.pagesize variables.
Form
No
On the action page of a form and in custom tags
called by the action page; cannot be used on a
form page that is not also the action page.
A form or cfform tag. Contains the values of
form field tags (such as input) in the form body
when the form is submitted. The variable name is
the name of the form field.
Request
Yes
On the creating page and in any pages invoked
during the current HTTP request after the variable is created, including in custom tags and
nested custom tags.
Specifying the prefix Request when you create
the variable.
Server
Yes
To any page on the ColdFusion server. Surround
all code that uses server variables in cflock
blocks.
Specifying the prefix Server when you create the
variable.
Session
Yes
For one client in one application and one browser Specifying the prefix Session when you create
session. Surround code that uses Session scope the variable.
variables in cflock blocks.
ADOBE COLDFUSION 8 45
ColdFusion Developer’s Guide
Scope prefix
Prefix required to
reference
Where available
Created by
Yes
Within a ColdFusion component or the body of a
user-defined function that was created using the
cffunction tag and put in an object, structure,
or scope. In the containing page, through the
component instance or containing object.
Within the component or function by specifying
the prefix This when you create the variable.
(type)
This
In the containing page, by specifying the component instance or object that contains the function as a prefix when you create the variable.
ThisTag
Yes
On the custom tag page.
Specifying the prefix ThisTag when you create
the variable in the tag or using the
cfassociate tag in a nested custom tag.
Thread
The thread name.
Any code in the request.
Using the keyword thread or the thread name
as a prefix when you create the variable.
Inside the thread
that creates the variable, you can also
use the keyword
You can create Thread variables only inside the
thread.
thread.
thread-local
(no prefix)
none
Within a thread created by the cfthread tag
Using no prefix when you create the variable.
You can also use the keyword var before the variable name.
URL
No
On the target page of the URL.
The system. Contains the parameters passed in
the URL query string used to access the page.
Variables
No
On the current page. Cannot be accessed by a
form’s action page (unless the form page is also
the action page). Variables in this scope used on
a page that calls a custom tag can be accessed in
the custom tag by using its Caller scope; however,
they are not available to any nested custom tags.
Specifying the prefix Variables, or using no prefix,
when you create the variable. (To create a Variables scope variable inside a ColdFusion thread,
you must use the Variables prefix.)
(Local)
Using scopes
The following sections provide details on how you can create and use variables in different scopes.
Evaluating unscoped variables
If you use a variable name without a scope prefix, ColdFusion checks the scopes in the following order to find the
variable:
1
Function local (UDFs and CFCs only)
2
Thread local (inside threads only)
3
Arguments
4
Variables (local scope)
5
Thread
6
CGI
7
Cffile
8
URL
9
Form
10 Cookie
ADOBE COLDFUSION 8 46
ColdFusion Developer’s Guide
11 Client
Because ColdFusion must search for variables when you do not specify the scope, you can improve performance by
specifying the scope for all variables.
To access variables in all other scopes, you must prefix the variable name with the scope identifier.
Scopes and CFX tags
ColdFusion scopes do not apply to ColdFusion Extension (CFX) tags, custom tags that you write in a programming
language such as C++ or Java. The ColdFusion page that calls a CFX tag must use tag attributes to pass data to the
CFX tag. The CFX tag must use the Java Request and Response interfaces or the C++ Request class to get and return
data.
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 “Using Arrays and Structures” on page 68.
Important: 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 variable’s 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 “Requiring users to
enter values in form fields” on page 517.
ADOBE COLDFUSION 8 47
ColdFusion Developer’s Guide
Testing for a variable’s existence
Before relying on a variable’s 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.
For example, if you submit a form with an unsettled check box, the action page does not get a variable for the check
box. The following example from a form action page makes sure the Contractor check box Form variable exists
before using it:
Contractor: #Form.Contractor#
You must always enclose the argument passed to the IsDefined function in double-quotation marks. For more
information on the IsDefined function, see the CFML Reference.
If you attempt to evaluate a variable that you did not define, ColdFusion cannot process the page and displays an
error message. To help diagnose such problems, turn on debugging in the ColdFusion Administrator or use the
debugger in your editor. The Administrator debugging information shows which variables are being passed to your
application pages.
Variable existence considerations
If a variable is part of a scope that is available as a structure, you might get a minor performance increase by testing
the variable’s existence using the StructKeyExists function instead of the IsDefined function.
You can also determine which Form variables exist by inspecting the contents of the Form.fieldnames built-in
variable. This variable contains a list of all the fields submitted by the form. Remember, however, that form text fields
are always submitted to the action page, and might contain an empty string if the user did not enter data.
The IsDefined function always returns False if you specify an array or structure element using bracket notation.
For example, IsDefined("myArray[3]") always returns False, even if the array element myArray[3] has a value.
To check for the existence of an array element, use cftry, as in the following example:
< cfoutput>Items[1][2] does not exist
Using the cfparam tag
You can ensure that a variable exists by using the cfparam tag, which tests for the variable’s existence and optionally
supplies a default value if the variable does not exist. The cfparam tag has the following syntax:
Note: For information on using the type attribute to validate the parameter data type, see the CFML Reference.
There are two ways to use the cfparam tag to test for variable existence, depending on how you want the validation
test to proceed:
• With only the name attribute to test that a required variable exists. If it does not exist, the ColdFusion server stops
processing the page and displays an error message.
ADOBE COLDFUSION 8 48
ColdFusion Developer’s Guide
• With the name and default attributes to test for the existence of an optional variable. If the variable exists,
processing continues and the value is not changed. If the variable does not exist, it is created and set to the value of
the default attribute, and processing continues.
The following example shows how to use the cfparam tag to check for the existence of an optional variable and to
set a default value if the variable does not already exist:
Example: testing for variables
Using the cfparam tag with the name attribute is one way to clearly define the variables that a page or a custom tag
expects to receive before processing can proceed. This can make your code more readable, as well as easier to
maintain and debug.
For example, the following cfparam tags indicate that this page expects two form variables named StartRow and
RowsToFetch:
If the page with these tags is called without either one of the form variables, an error occurs and the page stops
processing. By default, ColdFusion displays an error message; you can also handle the error as described in
“Handling Errors” on page 246.
Example: setting default values
The following example uses the cfparam tag to see if optional variables exist. If they do exist, processing continues.
If they do not exist, the ColdFusion server creates them and sets them to the default values.
You can use the cfparam tag to set default values for URL and Form variables, instead of using conditional logic. For
example, you could include the following code on the action page to ensure that a SelectedDepts variable exists:
Validating data
It is often not sufficient that input data merely exists; it must also have the right format. For example, a date field must
have data in a date format. A salary field must have data in a numeric or currency format. There are many ways to
ensure the validity of data, including the following methods:
•
Use the cfparam tag with the type attribute to validate a variable.
•
Use the IsValid function to validate a variable.
•
Use the cfqueryparam tag in a SQL WHERE clause to validate query parameters.
•
Use cfform controls that have validation attributes.
•
Use a form input tag with a hidden attribute to validate the contents of a form input field.
Note: Data validation using the cfparam, cfqueryparam, and form tags is done by the server. Validation using
cfform tags and hidden fields is done using JavaScript in the user’s browser, before any data is sent to the server.
ADOBE COLDFUSION 8 49
ColdFusion Developer’s Guide
For detailed information on validating data in forms and variables, see “Validating Data” on page 553 For detailed
information on validating query parameters, see “Using cfqueryparam” on page 399.
Passing variables to custom tags and UDFs
The following sections describe rules for how data gets passed to custom tags and user-defined functions that are
written in CFML, and to CFX custom tags that are written in Java or C++.
Passing variables to CFML tags and UDFs
When you pass a variable to a CFML custom tag as an attribute, or to a user-defined function as an argument, the
following rules determine whether the custom tag or function receives its own private copy of the variable or only
gets a reference to the calling page’s variable:
• Simple variables and arrays are passed as copies of the data. If your argument is an expression that contains
multiple simple variables, the result of the expression evaluation is copied to the function or tag.
•
Structures, queries, and cfobject objects are passed as references to the object.
If the tag or function gets a copy of the calling page’s data, changes to the variable in the custom tag or function do
not change the value of the variable on the calling page. If the variable is passed by reference, changes to the variable
in the custom tag or function also change the value of the variable in the calling page.
To pass a variable to a custom tag, you must enclose the variable name in number signs. To pass a variable to a
function, do not enclose the variable name in number signs. For example, the following code calls a user-defined
function using three Form variables:
TOTAL INTEREST: #TotalInterest(Form.Principal, Form.AnnualPercent,Form.Months)#
The following example calls a custom tag using two variables, MyString and MyArray:
Passing variables to CFX tags
You cannot pass arrays, structures, or cfobject objects to CFX tags. You can pass a query to a CFX tag by using the
query attribute when calling the tag. ColdFusion normally converts simple data types to strings when passing them
to CFX tags; however, the Java Request Interface getIntAttribute method lets you get a passed integer value.
50
Chapter 4: Using Expressions and
Number Signs
In CFML, you create expressions by using number signs to indicate expressions in Adobe ColdFusion tags such as
cfoutput, in strings, and in expressions. You also use variables in variable names and strings to create dynamic
expressions, and dynamic variables.
Contents
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Using number signs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Dynamic expressions and dynamic variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Expressions
ColdFusion expressions consist of operands and operators. Operands are comprised of constants and variables.
Operators, such as the multiplication symbol, are the verbs that act on the operands; functions are a form of operator.
The simplest expression consists of a single operand with no operators. Complex expressions have multiple operators
and operands. The following are all ColdFusion expressions:
12
MyVariable
a++
(1 + 1)/2
"father" & "Mother"
Form.divisor/Form.dividend
Round(3.14159)
Operators act on the operands. Some operators, such as functions with a single argument, take a single operand.
Many operators, including most arithmetic and logical operators, take two operands. The following is the general
form of a two-operand expression:
Expression Operator Expression
Note that the operator is surrounded by expressions. Each expression can be a simple operand (variable or constant)
or a subexpression consisting of more operators and expressions. Complex expressions are built up using subexpressions. For example, in the expression (1 + 1)/2, 1 + 1 is a subexpression consisting of an operator and two operands.
Operator types
ColdFusion has four types of operators:
•
Arithmetic
•
Boolean
•
Decision (or comparison)
•
String
Functions also can be viewed as operators because they act on operands.
ADOBE COLDFUSION 8 51
ColdFusion Developer’s Guide
Arithmetic operators
The following table describes the arithmetic operators:
Operator
Description
+ - * /
Basic arithmetic: Addition, subtraction, multiplication, and division.
In division, the right operand cannot be zero.
++ --
Increment and decrement. Increase or decrease the variable by one.
These operators can be used for pre-incrementing or decrementing (as in x = + i), where the variable is changed
before it is used in the expression, or post-incrementing or decrementing (as in x = i++), where the value is
changed after it is used in the expression. If the value of the variable i is initially 7, for example, the value of x in x =
++i is 8 after expression evaluation, but in x=i++, the value of x is 7. In both cases, the value of i becomes 8.
These operators cannot be used with expressions that involve functions, as in f().a++. Also, you can use an expression such as -++x, but ---x and +++x cause errors, because their meanings are ambiguous. You can use parentheses to group the operators, as in -(--x) or +(++x), however.
+= -= *= /= %=
Compound assignment operators. The variable on the right is used as both an element in the expression and the
result variable. Thus, the expression a += b is equivalent to a = a +b.
An expression can have only one compound assignment operator.
+ -
Unary arithmetic: Set the sign of a number.
MOD
Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The
value to the right of the operator should be an integer; using a non-numeric value causes an error, and if you specify
a real number, ColdFusion ignores the fractional part (for example, 11 MOD 4.7 is 3).
or %
\
Integer division: Divide an integer by another integer. The result is also an integer; for example, 9\4 is 2. The right
operand cannot be zero.
^
Exponentiation: Return the result of a number raised to a power (exponent). Use the caret character (^) to separate
the number from the power; for example, 2^3 is 8. Real and negative numbers are allowed for both the base and the
exponent. However, any expression that equates to an imaginary number, such -1^.5 results in the string "-1.#IND.
ColdFusion does not support imaginary or complex numbers.
Boolean operators
Boolean, or logical, operators perform logical connective and negation operations. The operands of Boolean
operators are Boolean (True/False) values. The following table describes the Boolean operators:
Operator
Description
NOT
Reverse the value of an argument. For example, NOT True is False and vice versa.
or !
AND
or &&
OR
or ||
Return True if both arguments are True; return False otherwise. For example, True AND True is True, but True AND
False is False.
Return True if any of the arguments is True; return False otherwise. For example, True OR False is True, but False OR
False is False.
XOR
Exclusive or: Return True if one of the values is True and the other is False. Return False if both arguments are True or
both are False. For example, True XOR True is False, but True XOR False is True.
EQV
Equivalence: Return True if both operands are True or both are False. The EQV operator is the opposite of the XOR
operator. For example, True EQV True is True, but True EQV False is False.
IMP
Implication: The statement A IMP B is the equivalent of the logical statement “If A Then B.” A IMP B is False only if A is
True and B is False. It is True in all other cases.
ADOBE COLDFUSION 8 52
ColdFusion Developer’s Guide
Decision operators
The ColdFusion decision, or comparison, operators produce a Boolean True/False result. Many types of operation
have multiple equivalent operator forms. For example, IS and EQ perform the same operation. The following table
describes the decision operators:
Operator
Description
IS
Perform a case-insensitive comparison of two values. Return True if the values are identical.
EQUAL
EQ
IS NOT
NOT EQUAL
Opposite of IS. Perform a case-insensitive comparison of two values. Return True if the values
are not identical.
NEQ
CONTAINS
Return True if the value on the left contains the value on the right.
DOES NOT CONTAIN
Opposite of CONTAINS. Return True if the value on the left does not contain the value on the
right.
GREATER THAN
Return True if the value on the left is greater than the value on the right.
GT
LESS THAN
LT
GREATER THAN OR EQUAL TO
Opposite of GREATER THAN. Return True if the value on the left is smaller than the value on
the right.
Return True if the value on the left is greater than or equal to the value on the right.
GTE
GE
LESS THAN OR EQUAL TO
Return True if the value on the left is less than or equal to the value on the right.
LTE
LE
Note: In CFScript expressions only, you can also use the following decision operators. You cannot use them in expressions
in tags. == (EQ), != (NEQ), > (GT), < (LT), >= (GTE), and <= (LTE).
Decision operator rules
The following rules apply to decision operators:
• When ColdFusion evaluates an expression that contains a decision operator other than CONTAINS or DOES
NOT CONTAIN, it first determines if the data can be converted to numeric values. If they can be converted, it
performs a numeric comparison on the data. If they cannot be converted, it performs a string comparison. This can
sometimes result in unexpected results. For more information on this behavior, see “Evaluation and type conversion
issues” on page 39.
• When ColdFusion evaluates an expression with CONTAINS or DOES NOT CONTAIN it does a string
comparison. The expression A CONTAINS B evaluates to True if B is a substring of A. Therefore an expression such
as the following evaluates as True:
123.45 CONTAINS 3.4
1 When a ColdFusion decision operator compares strings, it ignores the case. As a result, the following expression
is True:
ADOBE COLDFUSION 8 53
ColdFusion Developer’s Guide
"a" IS "A"
2 When a ColdFusion decision operator compares strings, it evaluates the strings from left to right, comparing the
characters in each position according to their sorting order. The first position where the characters differ determines
the relative values of the strings. As a result, the following expressions are True:
"ab" LT "aba"
"abde" LT "ac"
String operators
String operators manipulate strings of characters. The following table describes the operators:
Operator
Description
&
Concatenates strings.
&=
Compound concatenation. The variable on the right is used as both an element in the concatenation operation and
the result variable. Thus, the expression a &= b is equivalent to a = a & b.
An expression can have only one compound assignment operator.
Note: In a Query of Queries, you use || as the concatenation operator.
Operator precedence and evaluation ordering
The order of precedence controls the order in which operators in an expression are evaluated. The order of precedence is as follows. (Some alternative names for operators, such as EQUALS and GREATER THAN OR EQUAL TO
are omitted for brevity.)
Unary +, Unary ^
*, /
\
MOD
+, &
EQ, NEQ, LT, LTE, GT, GTE, CONTAINS, DOES NOT CONTAIN, ==, !=, >, >=, <, <=
NOT, !
AND, &&
OR
XOR, ||
EQV
IMP
To enforce a nonstandard order of evaluation, you must parenthesize expressions. For example:
•
6 - 3 * 2 is equal to 0
•
(6 - 3) * 2 is equal to 6
You can nest parenthesized expressions. When in doubt about the order in which operators in an expression will be
evaluated, use parentheses to force the order of evaluation.
Using functions as operators
Functions are a form of operator. Because ColdFusion functions return values, you can use function results as
operands. Function arguments are expressions. For example, the following are valid expressions:
•
•
Rand()
UCase("This is a text: ") & ToString(123 + 456)
ADOBE COLDFUSION 8 54
ColdFusion Developer’s Guide
Function syntax
The following table shows function syntax and usage guidelines:
Usage
Example
No arguments
Function()
Basic format
Function(Data)
Nested functions
Function1(Function2(Data))
Multiple arguments
Function(Data1, Data2, Data3)
String arguments
Function('This is a demo')
Function("This is a demo")
Arguments that are expressions
Function1(X*Y, Function2("Text"))
All functions return values. In the following example, the cfset tag sets a variable to the value returned by the Now
function:
You can use the values returned by functions directly to create more complex expressions, as in the following
example:
Abs(Myvar)/Round(3.14159)
For more information on how to insert functions in expressions, see “Using number signs” on page 55.
Optional function arguments
Some functions take optional arguments after their required arguments. If omitted, all optional arguments default
to a predefined value. For example:
•
Replace("Eat and Eat", "Eat", "Drink") returns "Drink and Eat"
•
Replace("Eat and Eat", "Eat", "Drink", "All") returns "Drink and Drink"
The difference in the results is because the Replace function takes an optional fourth argument that specifies the
scope of replacement. The default value is “One,” which explains why only the first occurrence of “Eat” was replaced
with “Drink” in the first example. In the second example, a fourth argument causes the function to replace all occurrences of “Eat” with “Drink”.
Expression evaluation and functions
It is important to remember that ColdFusion evaluates function attributes as expressions before it executes the
function. As a result, you can use any ColdFusion expression as a function attribute. For example, consider the
following lines:
When ColdFusion server executes the second line, it does the following:
1
Determines that there is an expression with a string concatenation.
2
Evaluates the firstVariable variable as the string "we all need".
3
Concatenates "we all need" with the string " more sleep!" to get "we all need more sleep!".
4
Passes the string "we all need more sleep!" to the UCase function.
ADOBE COLDFUSION 8 55
ColdFusion Developer’s Guide
Executes the UCase function on the string argument "we all need more sleep!" to get "WE ALL NEED MORE
SLEEP!".
5
6
Assigns the string value "WE ALL NEED MORE SLEEP!" to the variable myStringVar.
ColdFusion completes steps 1-3 before invoking the function.
Using number signs
Number signs (#) have a special meaning in CFML. When the ColdFusion server encounters number signs in CFML
text, such as the text in a cfoutput tag body, it checks to see if the text between the number signs is either a variable
or a function.
Number signs are also called pound signs.
Is so, it replaces the text and surrounding number signs with the variable value or the result of the function.
Otherwise, ColdFusion generates an error.
For example, to output the current value of a variable named Form.MyFormVariable, you delimit (surround) the
variable name with number signs:
Value is #Form.MyFormVariable#
In this example, the variable Form.MyFormVariable is replaced with the value assigned to it.
Follow these guidelines when using number signs:
•
Use number signs to distinguish variables or functions from plain text.
• Surround only a single variable or function in number signs; for example, #Variables.myVar# or #Left(myString,
position)#. (However, a function in number signs can contain nested functions, such as #Left(trim(myString),
position)#.
• Do not put complex expressions, such as 1 + 2 in number signs. Although this is allowed in a cfoutput block,
such as One plus one is #1 + 1#, doing so mixes logic and presentation.
•
Use number signs only where necessary, because unneeded number signs slow processing.
The following sections provide more details on how to use number signs in CFML. For a description of using
number signs to create variable names, see “Using number signs to construct a variable name in assignments” on
page 59.
Using number signs in ColdFusion tag attribute values
You can put variables, functions, or expressions inside tag attributes by enclosing the variable or expression with
number signs. For example, if the variable CookieValue has the value "MyCookie", the following line sets the
cfcookie value attribute to "The value is MyCookie":
You can optionally omit quotation marks around variables used as attribute values as shown in the following
example:
However, surrounding all attribute values in quotation marks is more consistent with HTML coding style.
ADOBE COLDFUSION 8 56
ColdFusion Developer’s Guide
If you use string expressions to construct an attribute value, as shown in the following example, the strings inside the
expression use single quotation marks (') to differentiate the quotation marks from the quotation marks that
surround the attribute value.
Note: You do not need to use number signs when you use the cfset tag to assign one variable’s value to another value.
For example, the following tag assigns the value of the oldVar variable to the new variable, newVar: .
Using number signs in tag bodies
You can put variables or functions freely inside the bodies of the following tags by enclosing each variable or
expression with number signs:
•
cfoutput
•
cfquery
•
cfmail
For example:
Value is #Form.MyTextField#
The name is #FirstName# #LastName#.
The value of Cos(0) is #Cos(0)#
If you omit the number signs, the text, rather than the value, appears in the output generated by the cfoutput
statement.
Two expressions inside number signs can be adjacent to one another, as in the following example:
"Mo" and "nk" is #Left("Moon", 2)##Mid("Monkey", 3, 2)#
This code displays the following text:
"Mo" and "nk" is Monk
ColdFusion does not interpret the double number sign as an escaped # character.
Using number signs in strings
You can put variables or functions freely inside strings by enclosing each variable or expression with number signs;
for example:
ColdFusion automatically replaces the text with the value of the variable or the value returned by the function. For
example, the following pairs of cfset statements produce the same result:
ADOBE COLDFUSION 8 57
ColdFusion Developer’s Guide
If number signs are omitted inside the string, the text, rather than the value, appears in the string. For example, the
following pairs of cfset statements produce the same result:
As with the cfoutput statement, two expressions can be adjacent to each other in strings, as in the following
example:
The double-quotation marks around "Moon" and "Monkey" do not need to be escaped (as in ""Moon"" and
""Monkey""). This is because the text between the number signs is treated as an expression; it is evaluated before its
value is inserted inside the string.
Nested number signs
In a few cases, you can nest number signs in an expression. The following example uses nested number signs:
In this example, number signs are nested so that the values of the variables FirstName and LastName are inserted in
the string whose length the Len function calculates.
Nested number signs imply a complex expression that can typically be written more clearly and efficiently without
the nesting. For example, you can rewrite the preceding code example without the nested number signs, as follows:
The following achieves the same results and can further improve readability:
A common mistake is to put number signs around the arguments of functions, as in:
These statements result in errors. As a general rule, never put number signs around function arguments.
Using number signs in expressions
Use number signs in expressions only when necessary, because unneeded number signs reduce clarity and can
increase processing time. The following example shows the preferred method for referencing variables:
In contrast, the following example uses number signs unnecessarily and is less efficient than the previous statement:
ADOBE COLDFUSION 8 58
ColdFusion Developer’s Guide
Dynamic expressions and dynamic variables
This section discusses the advanced topics of dynamic expressions, dynamic evaluation, and dynamic variable
naming. Many ColdFusion programmers never encounter or need to use dynamic expressions. However, dynamic
variable naming is important in situations where the variable names are not known in advance, such as in shopping
cart applications.
This section also discusses the use of the IIf function, which is most often used without dynamic expressions. This
function dynamically evaluates its arguments, and you must often use the DE function to prevent the evaluation. For
more information on using the IIF function, see “Using the IIF function” on page 63.
Note: This section uses several tools and techniques that are documented in later chapters. If you are unfamiliar with
using ColdFusion forms, structures, and arrays, you should learn about these tools before reading this section.
About dynamic variables
Dynamic variables are variables that are named dynamically, typically by creating a variable name from a static part
and a variable part. For example, the following example dynamically constructs the variable name from a variable
prefix and a static suffix:
Using dynamic variables in this manner does not require dynamic evaluation.
About dynamic expressions and dynamic evaluation
In a dynamic expression, the actual expression, not just its variable values, is determined at execution time. In other
words, in a dynamic expression the structure of the expression, such as the names of the variables, not just the values
of the variables, gets built at runtime.
You create dynamic expressions using string expressions, which are expressions contained in strings, (that is,
surrounded with quotation marks). Dynamic evaluation is the process of evaluating a string expression. The
Evaluate and IIf functions, and only these functions, perform dynamic evaluation.
When ColdFusion performs dynamic evaluation it does the following:
1
Takes a string expression and treats it as a standard expression, as if the expression was not a string.
2
Parses the expression to determine the elements of the expression and validate the expression syntax.
3 Evaluates the expression, looking up any variables and replacing them with their values, calling any functions,
and performing any required operations.
This process enables ColdFusion to interpret dynamic expressions with variable parts. However, it incurs a
substantial processing overhead.
Dynamic expressions were important in early versions of ColdFusion, before it supported arrays and structures, and
they still can be useful in limited circumstances. However, the ability to use structures and the ability to use
associative array notation to access structure elements provide more efficient and easier methods for dynamically
managing data. For information on using arrays and structures, see “Using Arrays and Structures” on page 68.
Selecting how to create variable names
The following two examples describes cases when you need dynamic variable names:
ADOBE COLDFUSION 8 59
ColdFusion Developer’s Guide
• Form applications where the number and names of fields on the form vary dynamically. In this case, the form
posts only the names and values of its fields to the action page. The action page does not know all the names of the
fields, although it does know how the field names (that is, the variable names) are constructed.
•
If the following are true:
•
ColdFusion calls a custom tag multiple times.
•
The custom tag result must be returned to different variables each time.
•
The calling code can specify the variable in which to return the custom tag result.
In this case, the custom tag does not know the return variable name in advance, and gets it as an attribute value.
In both cases, it might appear that dynamic expressions using the Evaluate function are needed to construct the
variable names. However, you can achieve the same ends more efficiently by using dynamic variable naming, as
shown in “Example: a dynamic shopping cart” on page 64.
This does not mean that you must always avoid dynamic evaluation. However, given the substantial performance
costs of dynamic evaluation, you should first ensure that one of the following techniques cannot serve your purpose:
•
An array (using index variables)
•
Associative array references containing expressions to access structure elements
•
Dynamically generated variable names
Dynamic variable naming without dynamic evaluation
While ColdFusion does not always allow you to construct a variable name in-line from variable pieces, it does let you
to do so in the most common uses, as described in the following sections.
Using number signs to construct a variable name in assignments
You can combine text and variable names to construct a variable name on the left side of a cfset assignment. For
example, the following code sets the value of the variable Product12 to the string "Widget":
To construct a variable name this way, all the text on the left side of the equal sign must be in quotation marks.
This usage is less efficient than using arrays. The following example has the same purpose as the previous one, but
requires less processing:
Dynamic variable limitation
When you use a dynamic variable name in quotation marks on the left side of an assignment, the name must be
either a simple variable name or a complex name that uses object.property notation (such as MyStruct.#KeyName#).
You cannot use an array as part of a dynamic variable name. For example, the following code generates an error:
productClassNo = 1>
productItemNo = 9>
"myArray[#productClassNo##productItemNo#]" = "Widget">
ADOBE COLDFUSION 8 60
ColdFusion Developer’s Guide
However, you can construct an array index value dynamically from variables without using quotation marks on the
left side of an assignment. For example, the preceding sample code works if you replace the final line with the
following line:
Dynamically constructing structure references
The ability to use associative array notation to reference structures provides a way for you to use variables to dynamically create structure references. (For a description of associative array notation, see “Structure notation” on
page 79.) Associative array structure notation allows you to use a ColdFusion expression inside the index brackets.
For example, if you have a productName structure with keys of the form product_1, product_2 and so on, you can
use the following code to display the value of productName.product_3:
Product_3 Name: #ProductName["product_" & prodNo]#
For an example of using this format to manage a shopping cart, see “Example: a dynamic shopping cart” on page 64.
Using dynamic evaluation
The following sections describe how to use dynamic evaluation and create dynamic expressions.
ColdFusion dynamic evaluation functions
The following table describes the functions that perform dynamic evaluation and are useful in evaluating dynamic
expressions:
Function
Purpose
DE
Escapes any double-quotation marks in the argument and wraps the result in double-quotation
marks. The DE function is particularly useful with the IIF function, to prevent the function from
evaluating a string to be output.
For an example of using the DE function with the IIF function, see “Using the IIF function” on
page 63.
Evaluate
Takes one or more string expressions and dynamically evaluates their contents as expressions
from left to right. (The results of an evaluation to the left can have meaning in an expression to the
right.) Returns the result of evaluating the rightmost argument.
For more information on this function see “About the Evaluate function” on page 61.
IIf
Evaluates a boolean condition expression. Depending on whether this expression is True or False,
dynamically evaluates one of two string expressions and returns the result of the evaluation. The
IIF function is convenient for incorporating a cfif tag in-line in HTML.
For an example of using this function, see “Using the IIF function” on page 63.
PrecisionEvaluate
Operates identically to the Evaluate function, except that it can calculate arbitrary precision
decimal arithmetic. If one or more operands in an arithmetic expression are decimal numbers,
such as 12947834.986532, and are too long to be represented exactly by a ColdFusion numeric
data type, the function uses arbitrary-precision arithmetic to calculate the result, and return the
result as an arbitrarily long string of numbers. For more information about this function, see
PrecisionEvaluate in the CFML Reference.
SetVariable
Sets a variable identified by the first argument to the value specified by the second argument. This
function is no longer required in well-formed ColdFusion pages; see “SetVariable function considerations” on page 63.
ADOBE COLDFUSION 8 61
ColdFusion Developer’s Guide
Function argument evaluation considerations
It is important to remember that ColdFusion always evaluates function arguments before the argument values are
passed to a function:
For example, consider the following DE function:
#DE("1" & "2")#
You might expect this line to display """1"" & ""2""". Instead, it displays “12”, because ColdFusion processes the line
as follows:
1
Evaluates the expression "1" & "2" as the string “12”.
2
Passes the string "12" (without the quotation marks) to the DE function.
3
Calls the DE function, which adds literal quotation marks around the 12.
Similarly, if you use the expression DE(1 + 2), ColdFusion evaluates 1 + 2 as the integer 3 and passes it to the
function. The function converts it to a string and surrounds the string in literal quotation marks: “3”.
About the Evaluate function
The Evaluate function takes one or more string expressions, dynamically evaluates their contents as expressions
from left to right, and returns the result of evaluating the rightmost argument.
The following example shows the Evaluate function and how it works with ColdFusion variable processing:
#myVar2#
#myVar#
#Evaluate("myVar2")#
#Evaluate("myVar")#
#Evaluate(myVar2)#
#Evaluate(myVar)#
Reviewing the code
The following table describes how ColdFusion processes this code:
Code
Description
Sets the two variables to the following strings:
myVar
27/9
#myVar2#
#myVar#
Displays the values assigned to the variables, myVar and 27/9, respectively.
#Evaluate("myVar2")#
Passes the string "myvar2" (without the quotation marks) to the Evaluate function, which does the
following:
1 Evaluates it as the variable myVar2.
2 Returns the value of the myVar2 variable, the string "myvar" (without the quotation marks).
ADOBE COLDFUSION 8 62
ColdFusion Developer’s Guide
Code
Description
#Evaluate("myVar")#
Passes the string "myvar" (without the quotation marks) to the Evaluate function, which does the
following:
1
Evaluates it as the variable myVar.
2 Returns the value of the myVar variable, the string "27/9" (without the quotation marks).
#Evaluate(myVar2)#
Evaluates the variable myVar2 as the string "myVar" and passes the string (without the quotation
marks) to the Evaluate function. The rest of the processing is the same as in the previous line.
#Evaluate(myVar)#
Evaluates the variable myVar as the string "27/9" (without the quotation marks), and passes it to
the Evaluate function, which does the following:
1
Evaluates the string as the expression 27/9
2 Performs the division.
3 Returns the resulting value, 3
As you can see, using dynamic expressions can result in substantial expression evaluation overhead, and the code
can be confusing. Therefore, you should avoid using dynamic expressions wherever a simpler technique, such as
using indexed arrays or structures can serve your purposes.
Avoiding the Evaluate function
Using the Evaluate function increases processing overhead, and in most cases it is not necessary. The following
sections provide examples of cases where you might consider using the Evaluate function.
Example 1
You might be inclined to use the Evaluate function in code such as the following:
1 + 1 is #Evaluate(1 + 1)#
Although this code works, it is not as efficient as the following code:
1 + 1 is #Result#
Example 2
This example shows how you can use an associative array reference in place of an Evaluate function. This technique
is powerful because:
•
Most ColdFusion scopes are accessible as structures.
• You can use ColdFusion expressions in the indexes of associative array structure references. For more information on using associative array references for structures, see “Structure notation” on page 79.
The following example uses the Evaluate function to construct a variable name:
Product Name: #Evaluate("Form.product_#i#")#
This code comes from an example where a form has entries for an indeterminate number of items in a shopping cart.
For each item in the shopping cart there is a product name field. The field name is of the form product_1, product_2,
and so on, where the number corresponds to the product’s entry in the shopping cart. In this example, ColdFusion
does the following:
1
Replaces the variable i with its value, for example 1.
ADOBE COLDFUSION 8 63
ColdFusion Developer’s Guide
2
concatenates the variable value with "Form.product_", and passes the result (for Form.product_1) to the
Evaluate function, which does the remaining steps.
Parses the variable product_1 and generates an executable representation of the variable. Because ColdFusion
must invoke its parser, this step requires substantial processing, even for a simple variable.
3
4
Evaluates the representation of the variable, for example as "Air popper".
5
Returns the value of the variable.
The following example has the same result as the preceding example and is more efficient:
ProductName: #Form["product_" & i]#
In this code, ColdFusion does the following:
6 Evaluates the expression in the associative array index brackets as the string "product_" concatenated with the
value of the variable i.
7
Determines the value of the variable i; 1.
8
Concatenates the string and the variable value to get product_1.
Uses the result as the key value in the Form structure to get Form[product_1]. This associative array reference
accesses the same value as the object.attribute format reference Form.product_1; in this case, Air popper.
9
This code format does not use any dynamic evaluation, but it achieves the same effect, of dynamically creating a
structure reference by using a string and a variable.
SetVariable function considerations
You can avoid using the SetVariable function by using a format such as the following to set a dynamically named
variable. For example, the following lines are equivalent:
In the second line, enclosing the myVar#i# variable name in quotation marks tells ColdFusion to evaluate the name
and process any text in number signs as a variable or function. ColdFusion replaces the #i# with the value of the
variable i, so that if the value of i is 12, this code is equivalent to the line
For more information on this usage, see “Using number signs to construct a variable name in assignments” on
page 59.
Using the IIF function
The IIf function is a shorthand for the following code:
The function returns the value of the result variable. It is comparable to the use of the JavaScript and Java ? : operator,
and can result in more compact code. As a result, the IIF function can be convenient even if you are not using
dynamic expressions.
ADOBE COLDFUSION 8 64
ColdFusion Developer’s Guide
The IIF function requires the DE function to prevent ColdFusion from evaluating literal strings, as the following
example shows:
#IIf(IsDefined("LocalVar"), "LocalVar", DE("The variable is not defined."))#
If you do not enclose the string "The variable is not defined." in a DE function, the IIF function tries to evaluate the
contents of the string as an expression and generates an error (in this case, an invalid parser construct error).
The IIF function is useful for incorporating ColdFusion logic in-line in HTML code, but it entails a processing time
penalty in cases where you do not otherwise need dynamic expression evaluation.
The following example shows using IIF to alternate table row background color between white and gray. It also
shows the use of the DE function to prevent ColdFusion from evaluating the color strings.
hello #i#
This code is more compact than the following example, which does not use IIF or DE:
hello #i#
Example: a dynamic shopping cart
The following example dynamically creates and manipulates variable names without using dynamic expression
evaluation by using associative array notation.
You need to dynamically generate variable names in applications such as shopping carts, where the required output
is dynamically generated and variable. In a shopping cart, you do not know in advance the number of cart entries or
their contents. Also, because you are using a form, the action page only receives Form variables with the names and
values of the form fields.
ADOBE COLDFUSION 8 65
ColdFusion Developer’s Guide
The following example shows the shopping cart contents and lets you edit your order and submit it. To simplify
things, the example automatically generates the shopping cart contents using CFScript instead of having the user fill
the cart. A more complete example would populate a shopping cart as the user selected items. Similarly, the example
omits all business logic for committing and making the order.
Create the form
1 Create a file in your editor.
Shopping Cart
CartItems=4;
Cart = ArrayNew(1);
for ( i=1; i LE cartItems; i=i+1)
{
Cart[i]=StructNew();
Cart[i].ID=i;
Cart[i].Name="Product " & i;
Cart[i].SKU=i*100+(2*i*10)+(3*i);
Cart[i].Qty=3*i-2;
}
Your shopping cart has the following items.
You can change your order quantities.
If you don't want any item, clear the item's check box.
When you are ready to order, click submit.
Order?
Product
Code
Quantity
ADOBE COLDFUSION 8 66
ColdFusion Developer’s Guide
2
Save the file as ShoppingCartForm.cfm.
Reviewing the code
The following table describes the code:
Code
Description
CartItems=4;
Cart = ArrayNew(1);
for ( i=1; i LE #cartItems#; i=i+1)
{
Cart[i]=StructNew();
Cart[i].ID=i;
Cart[i].Name="Product " & i;
Cart[i].SKU=i*100+(2*i*10)+(3*i);
Cart[i].Qty=3*i-2;
}
Create a shopping cart as an array of structures, with each structure containing
the cart item ID, product name, SKU number, and quantity ordered for one item
in the cart. Populate the shopping cart by looping CartItems times and setting
the structure variables to arbitrary values based on the loop counter. A real application would set the Name, SKU, and Quantity values on other pages.
Order?
Product
Code
Quantity
Start the form and its embedded table. When the user clicks the submit button,
post the form data to the ShoppingCartAction.cfm page.
Loop through the shopping cart entries to generate the cart form dynamically.
For each loop, generate variables used for the form field name attributes by
appending the cart item ID (Cart[i].ID) to a field type identifier, such as "sku_".
Create the Submit button and end the form.
Create the Action page
1 Create a file in your editor.
The table formats the form neatly. The first table row contains the column
headers. Each following row has the data for one cart item.
Use a single name, "itemID", for all check boxes. This way, the itemID value posted
to the action page is a list of all the check box field values. The check box field
value for each item is the cart item ID.
Each column in a row contains a field for a cart item structure entry. The
passthrough attribute sets the product name and SKU fields to read-only; note
the use of single-quotation marks. (For more information on the cfinput tag
passthrough attribute, see the CFML Reference.) The check boxes are selected
by default.
ADOBE COLDFUSION 8 67
ColdFusion Developer’s Guide
2
Enter the following text:
Your Order
You have ordered the following items:
3
Save the file as ShoppingCartAction.cfm
4
Open ShoppingCartform.cfm in your browser, change the check box and quantity values, and click Submit.
Reviewing the code
The following table describes the code:
Code
Description
Run the CFML on this page only if it is called by submitting a form. This is not needed if there
are separate form and action pages, but is required if the form and action page were one
ColdFusion page.
Set the default Form.itemID to the empty string. This prevents ColdFusion from displaying an
error if the user clears all check boxes before submitting the form (so no product IDs are
submitted).
You have ordered the following
items:
Display the name, SKU number, and quantity for each ordered item.
The form page posts Form.itemID as a list containing the value attributes of all the check
boxes. These attributes contain the shopping cart item IDs for the selected cart items. Use the
list values to index a loop that outputs each ordered item.
Use associative array notation to access the Form scope as a structure and use expressions in
the array indexes to construct the form variable names. The expressions consist of a string
containing the field name’s field type prefix (for example, "sku_"), concatenated with the variable i, which contains the shopping cart ItemID number (which is also the loop index variable).
68
Chapter 5: Using Arrays and Structures
Adobe ColdFusion supports dynamic multidimensional arrays. Using arrays can enhance your ColdFusion application code.
ColdFusion also supports structures for managing lists of key-value pairs. Because structures can contain other
structures or complex data types as it values, they provide a flexible and powerful tool for managing complex data.
Contents
About arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Basic array techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Populating arrays with data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Array functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
About structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Creating and using structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Structure examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Structure functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
About arrays
Traditionally, an array is a tabular structure used to hold data, much like a spreadsheet table with clearly defined
limits and dimensions.
In ColdFusion, you typically use arrays to temporarily store data. For example, if your site lets users order goods
online, you can store their shopping cart contents in an array. This lets you make changes easily without committing
the information, which the user can change before completing the transaction, to a database.
Basic array concepts
Subsequent discussions of ColdFusion arrays are based on the following terms:
Array dimension: The relative complexity of the array structure.
Index: The position of an element in a dimension, ordinarily surrounded by square brackets: my1Darray[1],
my2Darray[1][1], my3Darray[1][1][1].
Array element: Data stored at an array index.
The simplest array is a one-dimensional array, similar to a row in a table. A one-dimensional array has a name (the
variable name) and a numerical index. The index number references a single entry, or cell, in the array, as the
following figure shows:
ADOBE COLDFUSION 8 69
ColdFusion Developer’s Guide
Thus, the following statement sets the value of the fifth entry in the one-dimensional array MyArray to “Robert”:
A basic two-dimensional (2D) array is like a simple table. A three-dimensional (3D) array is like a cube of data, and
so on. ColdFusion lets you directly create arrays with up to three dimensions. You can use multiple statements to
create arrays with more than three dimensions.
The syntax my2darray[1][3]="Paul" is the same as saying “My2dArray is a two-dimensional array and the value
of the array element index [1][3] is ‘Paul’”.
About ColdFusion arrays
ColdFusion arrays differ from traditional arrays, because they are dynamic. For example, in a conventional array,
array size is constant and symmetrical, whereas in a ColdFusion array, you can have rows of differing lengths based
on the data that is added or removed.
The following figures show the differences between traditional arrays and ColdFusion arrays using 2D arrays. The
differences between traditional and ColdFusion 3D arrays are similar, but much harder to show on a page.
A conventional 2D array is like a fixed-size table made up of individual cells, as the following figure shows:
The following figure represents a ColdFusion 2D array:
ADOBE COLDFUSION 8 70
ColdFusion Developer’s Guide
A ColdFusion 2D array is actually a one-dimensional array that contains a series of additional 1D arrays. Each of the
arrays that make up a row can expand and contract independently of any other column. Similarly, a ColdFusion 3D
array is essentially three nested sets of 1D arrays.
Dynamic arrays expand to accept data that you add to them and contract as you remove data from them.
Basic array techniques
Referencing array elements
You reference array elements by enclosing the index with brackets: arrayName[x] where x is the index that you want
to reference. In ColdFusion, array indexes are counted starting with position 1, which means that position 1 in the
firstname array is referenced as firstname[1]. For 2D arrays, you reference an index by specifying two coordinates:
myarray[1][1].
You can use ColdFusion variables and expressions inside the square brackets to reference an index, as the following
example shows:
Note: The IsDefined function does not test the existence of array elements. Instead, put any code that might try to
access an undefined array element in a try block and use a catch block to handle exceptions that arise if elements do not
exist.
ADOBE COLDFUSION 8 71
ColdFusion Developer’s Guide
Creating arrays
In ColdFusion, you can create arrays explicitly, by using a function to declare the array and then assigning it data, or
implicitly by using an assignment statement. You can create simple or complex, multidimensional arrays.
Creating arrays using functions
To create an array explicitly, you use the arrayNew function and specify the array dimensions, as in the following
example:
This line creates a two-dimensional array named myNewArray. You use this method to create an array with up to
three dimensions.
After you create an array, you add array elements, which you can then reference by using the element indexes.
For example, suppose you create a one-dimensional array called firstname:
The array firstname holds no data and is of an unspecified length. Next you add data to the array:
After you add these names to the array, it has a length of 3.
Creating arrays implicitly
To create an array implicitly, you do not use the ArrayNew function. Instead, you use a new variable name on the left
side of an assignment statement, and array notation on the right side of the statement, as in the following example:
This single statement is equivalent to the four statements used to create the firstname array in Creating arrays using
functions.
When you create an array implicitly, the right side of the assignment statement has square brackets ([]) surrounding
the array contents and commas separating the individual array elements. The elements can be literal values, such as
the strings in the example, variables, or expressions. If you specify variables, do not put the variable names in
quotation marks.
You can create an empty array implicitly, as in the following example:
You can also create an array implicitly by assigning a single entry, as the following example shows:
ColdFusion does not allow nested implicit creation of arrays, structures, or arrays and structures. Therefore, you
cannot create a multidimensional array in a single implicit statement. For example, neither of the following statements is valid:
ADOBE COLDFUSION 8 72
ColdFusion Developer’s Guide
You cannot use a dynamic variable when you create an array implicitly. For example, the following expression
generates an error:
Creating complex multidimensional arrays
ColdFusion supports dynamic multidimensional arrays. When you declare an array with the ArrayNew function,
you specify the number of dimensions. You can create an asymmetrical array or increase an existing array’s dimensions by nesting arrays as array elements.
It is important to know that when you assign one array (array1) to an element of another array (array2), array1 is
copied into array2. The original copy of array1 still exists, independent of array2. You can then change the contents
of the two arrays independently.
The best way to understand an asymmetrical array is by looking at it. The following example creates an asymmetric,
multidimensional array and the cfdump tag displays the resulting array structure. Several array elements do not yet
contain data.
biggerarray[1][1][1][10]=3>
biggerarray[2][1][1]=myotherarray>
biggerarray[2][1][1][4][2]="five deep">
biggestarray[3][1][1]=biggerarray>
biggestarray[3][1][1][2][3][1]="This is complex">
myarray[3]="Can you see me">
Note: The cfdump tag displays the entire contents of an array. It is an excellent tool for debugging arrays and arrayhandling code.
Reviewing the code
The following table describes the code:
ADOBE COLDFUSION 8 73
ColdFusion Developer’s Guide
Code
Description
Create three empty arrays, a 1D array, a 2D array, and a 3D array.
Make element [1][1][1] of the 3D biggerarray array be a
copy of the 1D array. Assign 3 to the [1][1][1][10] element of the
resulting array.
The biggerarray array is now asymmetric. For example, it does
not have a [1][1][2][1] element.
Make element [2][1][1] of the 3D array be the 2D array, and
assign the [2][1][1][4][2] element the value "five deep".
The biggerarray array is now even more asymmetric.
Create a second 3D array. Make the [3][1][1] element of this
array a copy of the biggerarray array, and assign element
[3][1][1][2][3][1].
The resulting array is very complex and asymmetric.
Assign a value to element [3] of myarray.
Use cfdump to view the structure of biggestarray and myarray.
Notice that the "Can you see me" entry appears in myarray, but
not in biggestarray, because biggestarray has a copy of the
original myarray values and is not affected by the change to
myarray.
Adding elements to an array
You can add an element to an array by assigning the element a value or by using a ColdFusion function.
Adding an array element by assignment
You can add elements to an array by defining the value of an array element, as shown in the following cfset tag:
If an element does not exist at the specified index, ColdFusion creates it. If an element already exists at the specified
index, ColdFusion replaces it with the new value. To prevent existing data from being overwritten, use the
ArrayInsertAt function, as described in the next section.
If elements with lower-number indexes do not exist, they remain undefined. You must assign values to undefined
array elements before you can use them. For example, the following code creates an array and an element at index 4.
It outputs the contents of element 4, but generates an error when it tries to output the (nonexistent) element 3.
myarray4: #myarray[4]#
myarray3: #myarray[3]#
Adding an array element with a function
You can use the following array functions to add data to an array:
ADOBE COLDFUSION 8 74
ColdFusion Developer’s Guide
Function
Description
ArrayAppend
Creates a new array element at the end of the array.
ArrayPrepend
Creates a new array element at the beginning of the array.
ArrayInsertAt
Inserts an array element at the specified index position.
Because ColdFusion arrays are dynamic, if you add or delete an element from the array, any higher-numbered index
values all change. For example, the following code creates a two element array and displays the array contents. It then
uses ArrayPrepend to insert a new element at the beginning of the array and displays the result. The data that was
originally in indexes 1 and 2 is now in indexes 2 and 3.
ArrayPrepend(myarray, "New First Element");
For more information about these array functions, see the CFML Reference.
Deleting elements from an array
Use the ArrayDeleteAt function to delete data from the array at a particular index, instead of setting the data value
to zero or an empty string. If you remove data from an array, the array resizes dynamically, as the following example
shows:
The array now has #ArrayLen(firstname)# indexes
The first entry is #firstname[1]#
The second entry is #firstname[2]#
The ArrayDeleteAt function removed the original second element and resized the array so that it has two entries,
with the second element now being the original third element.
ADOBE COLDFUSION 8 75
ColdFusion Developer’s Guide
Copying arrays
You can copy arrays of simple variables (numbers, strings, Boolean values, and date-time values) by assigning the
original array to a new variable name. You do not have to use ArrayNew to create the new array first. When you
assign the existing array to a new variable, ColdFusion creates a new array and copies the old array’s contents to the
new array. The following example creates and populates a two-element array. It then copies the original array,
changes one element of the copied array and dumps both arrays. As you can see, the original array is unchanged and
the copy has a new second element.
If your array contains complex variables (structures, query objects, or external objects such as COM objects)
assigning the original array to a new variable does not make a complete copy of the original array. The array structure
is copied; however, the new array does not get its own copy of the complex data, only references to it. To demonstrate
this behavior, run the following code:
Create an array that contains a structure.
Copy the array and dump it.
Change the values in the new array.
Contents of the original array after the changes:
Contents of the new array after the changes:
The change to the new array also changes the contents of the structure in the original array.
To make a complete copy of an array that contains complex variables, use the Duplicate function.
Populating arrays with data
Array elements can store any values, including queries, structures, and other arrays. You can use a number of
functions to populate an array with data, including ArraySet, ArrayAppend, ArrayInsertAt, and ArrayPrepend.
These functions are useful for adding data to an existing array.
In particular, you should master the following basic techniques:
•
Populating an array with the ArraySet function
ADOBE COLDFUSION 8 76
ColdFusion Developer’s Guide
•
Populating an array with the cfloop tag
•
Populating an array from a query
Populating an array with the ArraySet function
You can use the ArraySet function to populate a 1D array, or one dimension of a multidimensional array, with some
initial value, such as an empty string or zero. This can be useful if you need to create an array of a certain size, but
do not need to add data to it right away. One reason to do this is so that you can refer to all the array indexes. If you
refer to an array index that does not contain some value, such as an empty string, you get an error.
The ArraySet function has the following form:
ArraySet (arrayname, startrow, endrow, value)
The following example initializes the array myarray, indexes 1 to 100, with an empty string:
ArraySet (myarray, 1, 100, "")
Populating an array with the cfloop tag
The cfloop tag provides a common and very efficient method for populating an array. The following example uses
a cfloop tag and the MonthAsString function to populate a simple 1D array with the names of the months. A
second cfloop outputs data in the array to the browser.
#months[loopcount]#
Using nested loops for 2D and 3D arrays
To output values from 2D and 3D arrays, you must employ nested loops to return array data. With a one-dimensional
(1D) array, a single cfloop is sufficient to output data, as in the previous example. With arrays of dimension greater
than one, you need to maintain separate loop counters for each array level.
Nesting cfloop tags for a 2D array
The following example shows how to handle nested cfloop tags to output data from a 2D array. It also uses nested
cfloop tags to populate the array:
The values in my2darray are currently:
[#OuterCounter#][#InnerCounter#]:
ADOBE COLDFUSION 8 77
ColdFusion Developer’s Guide
#my2darray[OuterCounter][InnerCounter]#
Nesting cfloop tags for a 3D array
For 3D arrays, you simply nest an additional cfloop tag. (This example does not set the array values first to keep the
code short.)
[#Dim1#][#Dim2#][#Dim3#]:
#my3darray[Dim1][Dim2][Dim3]#
Populating an array from a query
When populating an array from a query, keep the following things in mind:
• You cannot add query data to an array all at once. A looping structure is generally required to populate an array
from a query.
• You can reference query column data using array-like syntax. For example, myquery.col_name[1] references
data in the first row in the col_name column of the myquery query.
•
Inside a cfloop query= loop, you do not have to specify the query name to reference the query’s variables.
You can use a cfset tag with the following syntax to define values for array indexes:
In the following example, a cfloop tag places four columns of data from a sample data source into an array, myarray.
SELECT Emp_ID, LastName, FirstName, Email
FROM Employees
ID: #MyArray[Counter][1]#,
LASTNAME: #MyArray[Counter][2]#,
ADOBE COLDFUSION 8 78
ColdFusion Developer’s Guide
FIRSTNAME: #MyArray[Counter][3]#,
EMAIL: #MyArray[Counter][4]#
This example uses the query object built-in variable CurrentRow to index the first dimension of the array.
Array functions
The following functions are available for creating, editing, and handling arrays:
Function
Description
ArrayAppend
Appends an array element to the end of a specified array.
ArrayAvg
Returns the average of the values in the specified array.
ArrayClear
Deletes all data in a specified array.
ArrayDeleteAt
Deletes an element from a specified array at the specified index and resizes the array.
ArrayInsertAt
Inserts an element (with data) in a specified array at the specified index and resizes the array.
ArrayIsDefined
Returns True if the specified array is defined.
ArrayIsEmpty
Returns True if the specified array is empty of data.
ArrayLen
Returns the length of the specified array.
ArrayMax
Returns the largest numeric value in the specified array.
ArrayMin
Returns the smallest numeric value in the specified array.
ArrayNew
Creates an array of specified dimension.
ArrayPrepend
Adds an array element to the beginning of the specified array.
ArrayResize
Resets an array to a specified minimum number of elements.
ArraySet
Sets the elements in a 1D array in a specified range to a specified value.
ArraySort
Returns the specified array with elements sorted numerically or alphanumerically.
ArraySum
Returns the sum of values in the specified array.
ArraySwap
Swaps array values in the specified indexes.
ArrayToList
Converts the specified 1D array to a list, delimited with the character you specify.
IsArray
Returns True if the value is an array.
ListToArray
Converts the specified list, delimited with the character you specify, to an array.
For more information about each of these functions, see the CFML Reference.
About structures
ColdFusion structures consist of key-value pairs. Structures let you build a collection of related variables that are
grouped under a single name. You can define ColdFusion structures dynamically.
ADOBE COLDFUSION 8 79
ColdFusion Developer’s Guide
You can use structures to refer to related values as a unit, rather than individually. To maintain employee lists, for
example, you can create a structure that holds personnel information such as name, address, phone number, ID
numbers, and so on. Then you can refer to this collection of information as a structure called employee rather than
as a collection of individual variables.
A structure’s key must be a string. The values associated with the key can be any valid ColdFusion value or object. It
can be a string or integer, or a complex object such as an array or another structure. Because structures can contain
any kind of data they provide a very powerful and flexible mechanism for representing complex data.
Structure notation
ColdFusion supports three types of notation for referencing structure contents. The notation that you use depends
on your requirements.
Notation
Description
Object.property
You can refer to a property, prop, of an object, obj, as obj.prop. This notation, also called dot notation, is useful
for simple assignments, as in this example:
depts.John="Sales"
Use this notation only when you know the property names (keys) in advance and they are strings, with no
special characters, numbers, or spaces. You cannot use the dot notation when the property, or key, is dynamic.
Associative arrays
If you do not know the key name in advance, or it contains spaces, numbers, or special characters, you can use
associative array notation. This notation uses structures as arrays with string indexes; for example:
depts["John"]="Sales"
depts[employeeName]="Sales"
You can use a variable (such as employeeName) as an associative array index. Therefore, you must enclose any
literal key names in quotation marks.
For information on using associative array references containing variables, see “Dynamically constructing
structure references” on page 60.
Structure
You use structure notation only when you create structures and set their initial values, not when you are
accessing or updating structure data, and only on the right side of an assignment expression. This notation has
the following format:
{keyName=value[,keyName=value]...}
where the square braces ([]) and ellipses (...) indicate optional contents that can be repeated.
The following example creates a structure that uses structure notation:
ADOBE COLDFUSION 8 80
ColdFusion Developer’s Guide
key2Var="key2">
key3Var="key3">
var2="2">
Value of the first key
#mystruct.key1#
#mystruct["key1"]#
#mystruct[key1Var]#
Value of the second entry in the key2 array
#myStruct.key2[2]#
#myStruct["key2"][2]#
#myStruct[key2Var][2]#
#myStruct[key2Var][var2]#
Value of the struct2key2 entry in the key3 structure
#myStruct.key3.struct2key2#
#myStruct["key3"]["struct2key2"]#
#myStruct[key3Var]["struct2key2"]#
#myStruct.key3["struct2key2"]#
#myStruct["key3"].struct2key2#
Reviewing the code
The following table describes the code:
Code
myArray[1]="2">
myArray[2]="3">
myStruct2=StructNew()>
myStruct2.struct2key1="4">
myStruct2.struct2key2="5">
myStruct=StructNew()>
myStruct.key1="1">
myStruct.key2=myArray>
myStruct.key3=myStruct2>
Create a structure with three entries: a string, an array, and an embedded structure.
Display the complete structure.
key2Var="key2">
key3Var="key3">
var2="2">
ADOBE COLDFUSION 8 81
ColdFusion Developer’s Guide
Code
Description
Value of the first key
#mystruct.key1#
#mystruct["key1"]#
#mystruct[key1Var]#
Output the value of the structure’s key1 (string) entry using the following notation:
Value of the second entry in the
key2 array
#myStruct.key2[2]#
#myStruct["key2"][2]#
#myStruct[key2Var][2]#
#myStruct[key2Var][var2]#
•
object.property notation
•
associative array notation with a constant
•
associative array notation with a variable
Output the value of the second entry in the structure’s key2 array using the
following notation:
•
object.property notation
•
associative array notation with a constant
•
associative array notation with a variable
•
associative array notation with variables for both the array and the array
index
Value of the struct2key2 entry in
the key3 structure
#myStruct.key3.struct2key2#
#myStruct["key3"]["struct2key2"]#
#myStruct[key3Var]["struct2key2"]#
#myStruct.key3["struct2key2"]#
#myStruct["key3"].struct2key2#
Output the value of second entry in the structure’s key3 embedded structure
using the following notation:
•
object.property notation
•
associative array notation with two constants
•
associative array notation with a variable and a constant
•
object.property notation followed by associative array notation
•
associative array notation followed by object.property notation
Creating and using structures
This section explains how to create and use structures in ColdFusion. The sample code in this section uses a
structure called employee, which is used to add new employees to a corporate information system.
Creating structures
In ColdFusion, you can create structures explicitly by using a function, and then populate the structure using
assignment statements or functions, or you can create the structure implicitly by using an assignment statement.
Creating structures using functions
You can create structures by assigning a variable name to the structure with the StructNew function as follows:
For example, to create a structure named departments, use the following syntax:
This creates an empty structure to which you can add data.
Creating structures implicitly
You can create an empty structure implicitly, as in the following example:
ADOBE COLDFUSION 8 82
ColdFusion Developer’s Guide
You can also create a structure by assigning data to a variable. For example, each of the following lines creates a
structure named myStruct with one element, name, that has the value Adobe Systems Incorporated.
When you use structure notation to create a structure, as shown in the third example, you can populate multiple
structure fields. The following example shows this use:
Similarly, you cannot use object.property notation on the left side of assignments inside structure notation. The
following statement, for example, causes an error:
Instead of using these formats, you must use multiple statements, such as the following:
You cannot use a dynamic variable when you create a structure implicitly. For example, the following expression
generates an error:
Adding and updating structure elements
You add or update a structure element to a structure by assigning the element a value or by using a ColdFusion
function. It is simpler and more efficient to use direct assignment.
You can add structure key-value pairs by defining the value of the structure key, as the following example shows:
The following code uses cfset and object.property notation to create a structure element called departments.John,
and changes John’s department from Sales to Marketing. It then uses associative array notation to change his
department to Facilities. Each time the department changes, it displays the results:
Before the first change, John was in the #departments.John# Department
After the first change, John is in the #departments.John# Department
After the second change, John is in the #departments.John# Department
ADOBE COLDFUSION 8 83
ColdFusion Developer’s Guide
Getting information about structures and keys
You use ColdFusion functions to find information about structures and their keys.
Getting information about structures
To find out if a given value represents a structure, use the IsStruct function, as follows:
IsStruct(variable)
This function returns True if variable is a ColdFusion structure. (It also returns True if variable is a Java object that
implements the java.util.Map interface.)
Structures are not indexed numerically, so to find out how many name-value pairs exist in a structure, use the
StructCount function, as in the following example:
StructCount(employee)
To discover whether a specific Structure contains data, use the StructIsEmpty function, as follows:
StructIsEmpty(structure_name)
This function returns True if the structure is empty, and False if it contains data.
Finding a specific key and its value
To determine whether a specific key exists in a structure, use the StructKeyExists function, as follows:
StructKeyExists(structure_name, "key_name")
Do not put the name of the structure in quotation marks, but you do put the key name in quotation marks. For
example, the following code displays the value of the MyStruct.MyKey only if it exists:
#mystruct.myKey#
You can use the StructKeyExists function to dynamically test for keys by using a variable to represent the key
name. In this case, you do not put the variable in quotation marks. For example, the following code loops through
the records of the GetEmployees query and tests the myStruct structure for a key that matches the query’s LastName
field. If ColdFusion finds a matching key, it displays the Last Name from the query and the corresponding entry in
the structure.
#LastName#: #mystruct[LastName]#
If the name of the key is known in advance, you can also use the ColdFusion IsDefined function, as follows:
IsDefined("structure_name.key")>
However, if the key is dynamic, or contains special characters, you must use the StructKeyExists function.
Note: Using StructKeyExists to test for the existence of a structure entry is more efficient than using IsDefined.
ColdFusion scopes are available as structures and you can improve efficiency by using StructKeyExists to test for the
existence of variables.
Getting a list of keys in a structure
To get a list of the keys in a CFML structure, you use the StructKeyList function, as follows:
ADOBE COLDFUSION 8 84
ColdFusion Developer’s Guide
You can specify any character as the delimiter; the default is a comma.
Use the StructKeyArray function to returns an array of keys in a structure, as follows:
Note: The StructKeyList and StructKeyArray functions do not return keys in any particular order. Use the
ListSort or ArraySort functions to sort the results.
Copying structures
ColdFusion provides several ways to copy structures and create structure references. The following table lists these
methods and describes their uses:
Technique
Use
Duplicate function Makes a complete copy of the structure. All data is copied from the original structure to the new structure, including
the contents of structures, queries, and other objects. As a result changes to one copy of the structure have no effect
on the other structure.
This function is useful when you want to move a structure completely into a new scope. In particular, if a structure is
created in a scope that requires locking (for example, Application), you can duplicate it into a scope that does not
require locking (for example, Request), and then delete it in the scope that requires locking.
StructCopy func-
tion
Makes a shallow copy of a structure. It creates a new structure and copies all simple variable and array values at the
top level of the original structure to the new structure. However, it does not make copies of any structures, queries,
or other objects that the original structure contains, or of any data inside these objects. Instead, it creates a reference
in the new structure to the objects in the original structure. As a result, any change to these objects in one structure
also changes the corresponding objects in the copied structure.
The Duplicate function replaces this function for most, if not all, purposes.
Variable assignment
Creates an additional reference, or alias, to the structure. Any change to the data using one variable name changes
the structure that you access using the other variable name.
This technique is useful when you want to add a local variable to another scope or otherwise change a variable’s
scope without deleting the variable from the original scope.
The following example shows the different effects of copying, duplicating, and assigning structure variables:
Create a new structure