Using Variable Types ColdFusion Help

Cold Fusion is different from many programming environments in that it has many types of variables. These types differ from each other in terms of their sources and how they are created. There are also differences in how the variables are referenced Nonetheless all of these types of variables are fundamentally used in the same way: They can be used in the same expressions they can be used to create dynamic C’ output and they can be used as the basis of dynamic queries.

The following list outlines the types of variables found in Cold fusion

URL Variables reflecting parameters appended to a URL
CGI Variables reflecting the CGI environment variables of the context of the requested template
Cookie Variables reflecting cookies passed to the template by the requesting browser
Query Variables reflecting the results of a query
Form Variables reflecting the values of the fields in a form submitted to the current template
Client Variables used to associate data with a specific client
Server Variables used to associate data with a specific Web server
Session Variables used to associate data with a given client session
Application Variables used to associate data with a specific Cold Fusion application

Client server session and application variables are discussed in more detail in Chapter 17 “Implementing the ColdFusion Web Application Framework.” Form variables are detail in Chapter 15 “Forms” and query variables are considered

throughout the book as we work with different types of queries (they are first discussed in Chapter 3).

this chapter you will look at local URL CGI and cookie variables in detail


The CFSET tag is the basic mechanism for creating local variables in a template. Using this tag, you can create a new variable and assign a simple value to it or to the result of a complex expression.

For instance suppose you are developing a mathematical application that will use the value of pi throughout the application. Rather. than repeatedly using the number 3.1415 (an approximation of pi) throughout the template you can simply create a variable called PI to hold the value and then reference that variable throughout your template. You do this with the following tag:

<CFSET PI – 3.1415>

This simple example highlights the bask structure of the CFSET tag:

<CFSET Variable_name – Expression>

The expression can be anything from a simple value such as 3.1415 to another variable to a compound expression including values variables and operators.

The CFSET tag is not only used in the creation of new variables. It also can be used to . assign new values to a variable. For instance if after creating your PI variable you want to change it to a less accurate value such as 3.14 you simply use another CFSET tag:

<CFSET PI – 3.14>

To·better understand this, consider the following segment of code:

<CFSET PI – 3.1415>
<CFSET PI-3.14>

This code produces the following output


After you used the second CFSETtag, the value of the PI variable was changed for all subsequent lines of the template.

When assigning values to a variable you can also use expressions. For instance if you want to calculate the circumference of a circle with a diameter of 10 you could use the following tag

<CFSET Area – PI * 10>

Notice the use of the PI variable to the right of the CFSETtag (in the expression

An interesting feature of the CFSETtag is the capability to dynamically specify the variable name. As an example consider a situation in which the name of the desired variable is stored in a variable itself. Then say you want to assign a value to the specified
variable. This situation works something like this

<CFSET DesiredVariable – ‘PI’>
<CFSET ‘tDes;redVariablel’ – 3.1415>

Notice the use of “#Des redvar iable#’ on the left side of the second CFSETtag tocreate a variable called PI, the value of Des; red Variable

When creating your own variables using CFSET there are some limitations to the names of-the variables you create

• Names should begin with a letter.
• Names can include numbers letters or the underscore character (but cannot begin with a number). All other characters are invalid in variable names. These invalid characters include spaces dashes and punctuation such as dots and commas.
•Names are not case sensitive. For instance TestName te.stname and TESTNAME all refer to the same variable.
• Names should not be the same as reserved words in ColdFusion. Reserved words include function names and operators from expressions Functions are discussed’ in Chapter 5 “Functions” and expressions are discussed in Chapter 6.

Posted on November 14, 2015 in Creating and Manipulating Variables

Share the Story

Back to Top
Share This