ColdFusion Variables

Contact Us or call 1-877-932-8228
ColdFusion Variables

ColdFusion Variables

ColdFusion is weakly typed, meaning that you do not have to specify the data type that a variable holds. Rather, ColdFusion determines the data type of a variable at the time the variable is used. A common way to set a variable is with the <cfset> tag.

<cfset firstname = "Paul"> <cfset age = 63>

Variable Names

In ColdFusion, variable names:

  • consist of letters, digits, underscores, and dollar signs
  • cannot begin with a digit
  • are case-insensitive

Variable Prefixes and Scope

There are many different flavors of variables in ColdFusion:

  • variables specific to a page
  • variables specific to a user's session
  • variables specific to an entire application
  • etc.

These flavors of variables each have a different scope, which determines where and how long a variable exists. For example, variables specific to a user's session are of the Session scope. They exist for the duration of the session and are accessible on every page that is a part of the session. Variables specific to a page (i.e, Local variables) are of the Variables scope.

A prefix is used to specify the scope of a variable. The syntax is shown below.


The following table shows several variable scopes and their prefixes. As shown in the table, not all prefixes are required.

Variable Scope
Scope Prefix Description
Variables VARIABLES Defined and accessible on the current page only.
CGI CGI Accessible on any page. Contains server environment variables.
URL URL Accessible on the current page. Contains values passed in on the query string.
Form FORM Accessible on the current page. Contains values passed in through a form.
Cookie COOKIE Accessible on any page. Contains variables held in cookies on the client machine.
Client CLIENT Accessible on any page. Contains variables created using the Client prefix.
Arguments ARGUMENTS Defined and accessible within a user-defined function or a ColdFusion Component method.
Session SESSION Prefix Required. Accessible on any page to a single user for the duration of a client session.
Application APPLICATION Prefix Required. Accessible on any page to all users until the application is restarted.
Server SERVER Prefix Required. Accessible on any page that is delivered from specific server.

Variables of different scopes can have the same names. For example, a local variable could have the same name as a form variable. If such a variable is referred to without a prefix, ColdFusion needs to know which variable to use. ColdFusion will search through the scopes in the following order:

  1. Arguments
  2. Local (Variables)
  3. CGI
  4. URL
  5. Form
  6. Cookie
  7. Client

In general, it is a good idea to include the prefix when referring to variables as this will help avoid confusion and could make the page process more quickly as ColdFusion will know exactly where to look for the variable.

Using <cfoutput>

A ColdFusion page is generally a mixture of CFML tags, HTML tags, and plain text. ColdFusion ignores any content that is not contained within a CFML tag. That is, content outside of CFML tags is returned as is to the browser.

The <cfoutput> tag is used to tell ColdFusion to attempt to evaluate any variable or expressions contained within pound signs (#). Let's take a look at an example. You guessed it - "Hello World".

Code Sample:

               <cfset VARIABLES.greeting="Hello">

	<title>Hello World!</title>
	<cfoutput>#VARIABLES.greeting# World!</cfoutput>

The code isn't very exciting. In fact, ColdFusion doesn't buy us anything here as we could have just as easily output the code using HTML. There is nothing dynamic about the script. Let's try something a little more interesting.

Passing Variables on the URL

A common way to pass values from the browser to the server is by appending them to the URL as follows:

The part of the url that follows the question mark is called the query string. One or more name-value pairs can be passed to the server in this way. Each name-value pair is separated by an ampersand (&). The processing page can read these name-value pairs and use them to determine its response.

The HTML page below shows an example of how these name-value pairs might be passed.

Code Sample:

	<title>Preferred Greeting</title>
	Do you prefer a formal greeting or an informal greeting?
	<li><a href="HelloHi.cfm?greet=Hello">Formal</a></li>
	<li><a href="HelloHi.cfm?greet=Hi">Informal</a></li>
	<li><a href="HelloHi.cfm?greet=Howdy">Friendly</a></li>

Code Sample:

	<title><cfoutput>#URL.greet#</cfoutput> World!</title>
<cfoutput>#URL.greet# World!</cfoutput>

The <cfoutput> tags tell ColdFusion to look for variables and expressions to evaluate. Within <cfoutput> tags, any content not in pound signs (#) is simply ignored. Another way to write this page would be to replace the two sets of <cfoutput> tags with an open <cfoutput> tag before the open <html> tag and a close </cfoutput> tag after the close </html> tag.

Note that <cfoutput> tags, as a general rule, cannot be nested.

ColdFusion Comments

ColdFusion comments are similar to HTML comments except that they take three dashes instead of two.

<!-- This is an HTML comment --> <!--- This is a ColdFusion comment --->