ColdFusion Variable Types

ColdFusion variables come in many different types. Firstly, all variables belong to a scope. There are many different scopes and each variable created in your application belongs to one of these scopes. Secondly, all variables can be either local or global. Thirdly, all variables are either persistent or non-persistent.

Variable Scopes

A variable's scope is determined by its origin. The scope determines a number of properties about the variable, such as its life span, timeout, and storage location, and therefore, how it can be used.

Below is a list of the available scopes for ColdFusion variables:

ScopeDescription
VariablesThis scope refers to local variables that are not specifically reserved for use in custom tags. If you set a variable without specifying a scope (i.e. <cfset variableName = "">, it will automatically belong to the variables scope.
AttributesThis scope is used within a custom tag. When you pass an attribute to a custom tag, it becomes available in the custom tag within the attributes scope.
CallerUsed within a custom tag to set or read variables within the template that called it.
ArgumentsUsed within a function to refer to arguments that were passed in by the calling template.
ThisUsed within a component to store its own properties.
RequestThe Request scope is used for the current request. These variables are non-persistent global variables.
CGICGI variables describe the current requests environment, are created automatically, and are read-only. You can't modify a CGI variable, only read it.
FormVariables submitted from a form using the "Post" method become part of the Form scope.
URLVariables passed through the URL are part of the URL scope.
ServerServer scope variables are available to all applications on the current server.
ApplicationApplication scope variables are available to a whole application (as defined in the <cfapplication name=""... tag within the Application.cfm file).
SessionThe Session scope is available for the life of a user's session. The length of the lifespan can be determined by a given period of inactivity. This period can be determined in the cfapplication tag. For example, to set a timeout after 20 minutes of inactivity: <cfapplication ... sessiontimeout = "#CreateTimeSpan(0,0,20,0)#"
ClientClient variables are stored on the server, either in the registry or a database (but can also be stored in a cookie on the client machine).
CookieCookie variables are global, and persistent, variables stored on the user's machine.

"Scoping" your Variables

When you set or read a variable, it is good practice to tell ColdFusion which scope it belongs to. Although this is not required, it will avoid any confusion around whether the correct variable is being used or not.

You may occasionally encounter two or more variables with the same name, but belonging to a different scope. To avoid the wrong one being used, you should scope your variables.

You scope a variable simply by prefixing the variable name with the name of the scope (and separating them with a dot).

For example, to scope a session variable, you would do something like this:

Variable Persistence

The following chart outlines the different variable scopes, whether they're local or global, and indicates whether they are persistent variables or non-persistent.

 Non-PersistentPersistent
Local Variables
  • Variables
  • Attributes
  • Caller
  • Arguments
  • This
(none)
Global Variables
  • Request
  • CGI
  • Form
  • URL
  • Server
  • Application
  • Session
  • Client
  • Cookie