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.
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:
|Variables||This 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. |
|Attributes||This 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.|
|Caller||Used within a custom tag to set or read variables within the template that called it.|
|Arguments||Used within a function to refer to arguments that were passed in by the calling template.|
|This||Used within a component to store its own properties.|
|Request||The Request scope is used for the current request. These variables are non-persistent global variables.|
|CGI||CGI variables describe the current requests environment, are created automatically, and are read-only. You can't modify a CGI variable, only read it.|
|Form||Variables submitted from a form using the "Post" method become part of the Form scope.|
|URL||Variables passed through the URL are part of the URL scope.|
|Server||Server scope variables are available to all applications on the current server.|
|Application||Application scope variables are available to a whole application (as defined in the |
|Session||The 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: |
|Client||Client 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).|
|Cookie||Cookie 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:
The following chart outlines the different variable scopes, whether they're local or global, and indicates whether they are persistent variables or non-persistent.