Creating variables with the cfset tag

ColdFusion lets you create variables as you need them. You create the variable (name and value) using the cfset tag. This tag has the following syntax:

<cfset variable_name = value>

In the following examples, the variables are assigned a string literal value. All string literal values are surrounded by double-quotation marks.

<cfset my_first_name = "Kaleigh">
<cfset my_last_name = "Smith">

In the next example, ColdFusion uses the values of the my_first_name and my_last_name variables to set the value for the my_full_name variable in the last line of code. The ampersand (&) string operator joins the variables, and the space surrounded by double-quotation marks (" ") adds a space between the variables.

<cfset my_first_name = "Kaleigh">
<cfset my_last_name = "Smith">
<cfset my_full_name = variables.my_first_name & " " & variables.my_last_name>

Tip: String values assigned to a variable must be enclosed in single-quotation marks (') or double-quotation marks ("). Numeric or Boolean values assigned to a variable do not require single- or double-quotation marks.

So far, all the variable examples have shown local variables. Local variables are variables that you can use only on the current ColdFusion page. The previous example used a variables prefix to reference an existing variable on the page. Using a prefix when referencing a variable is important because ColdFusion supports many types of variables. Use the following syntax to reference a local variable:

variables.variablename

Because ColdFusion lets you use the same name with variables of more than one type, ColdFusion relies on scope referencing. In scope referencing, you preface the variable’s name with the scope when you refer to that variable.

Other variables and their scope

Each type of variable that ColdFusion supports has it own scope, or where it can be referenced, and its own way of referencing that variable type. The following table identifies some of the more common types of variables and their prefixes:

Scope

Prefix

Description

Variables

(local variable)

Variables

Variables created using a cfset or cfparam tag. Most often you define the variable on the current page or on a page that you include using the cfinclude tag.

Form

Form

Data entered in tags in an HTML form or ColdFusion form and processed on the action page.

URL

URL

Variables passed to a page as URL string parameters.

Query

QueryName

Variables that have names based on the column names that you select in the database table. The values are created when you execute the query that selects data from the database.

You will use these other types of variables in Part II of this manual. For additional information about variables, see CFML Reference.