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>

Usage

You use the cfset tag in several ways in your applications.

Calling functions

When you use the cfset tag to call a function, you do not have to assign the function return value to a variable if the function does not return a value or you do not have 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 Scores array:

<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>