User Guide

318 Chapter 2: ColdFusion Tags
cfset
Description
Sets a value in ColdFusion. Used to create a variable, if it does not exist, and assign it a value. Also
used to call functions.
Category
Variable manipulation tags
Syntax
<cfset
var variable_name = expression
>
See also
cfcookie, cfparam, cfregistry, cfsavecontent, cfschedule
Attributes
Usage
The following sections provide detailed descriptions of some of the uses for the cfset tag.
Calling Functions
When you use the cfset tag to call a function, you do not need to assign the function return value
to a variable if the function does not return a value or you do not need to use the value returned
by the function. For example, the following line is a valid ColdFusion cfset tag for deleting the
MyVariable variable from the Application scope:
<cfset StructDelete(Application, "MyVariable")>
Arrays
The following example assigns a new array to the variable months:
<cfset months = ArrayNew(1)>
This example creates a variable Array_Length that resolves to the length of the array Scores:
<cfset Array_Length = ArrayLen(Scores)>
This example assigns, to index position two in the array months, the value February:
<cfset months[2] = "February">
Dynamic variable names
In this example, the variable name is itself a variable:
<cfset myvariable = "current_value">
<cfset "#myvariable#" = 5>
Attribute Req/Opt Default Description
var Optional A keyword. Does not take a value. Identifies the
variable as being local to a function. The variable only
exists for the time of the current invocation of the
function.
variable_name Required Variable