Contents > Getting Started Building ColdFusion MX Applications > CFML Basics > Understanding CFML elements > Creating variables with the cfset tag PreviousNext

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. The syntax for this tag is:

<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 (') 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 shown have been about local variables. Local variables are variables that you can use only on the current ColdFusion page. As shown in the previous example, a Variables prefix was used to reference an existing variable on the page. Using a prefix when referencing a variable is important because ColdFusion supports many types of variables. The syntax for referencing a local variable is as follows:

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

ColdFusion supports many types of variables. Each type 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 cfset or cfparam. Most often you define the variable on the current page or on a page that you include using cfinclude.

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 are named 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 book. For additional information about variables, see CFML Reference.


Contents > Getting Started Building ColdFusion MX Applications > CFML Basics > Understanding CFML elements > Creating variables with the cfset tag PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/cfml_b12.htm