Everything You Must Know About ColdFusion Variables - Scope of a variable
(Page 5 of 7 )
In general, scope refers to the time during which the variable persists (and therefore can be accessed); that is its lifetime. Some variables may last only for the duration of a session, some may last until the application is terminated. Some variables may be available in a certain portion of a program, generally inside a function, and not available outside the function. Compared to the scope of a variable in JavaScript, ColdFusion has more fine grained scope definition for its variables.
Scopes and their typesThe scope types are listed here (taken directly from the Macromedia website) but their usage will be discussed at appropriate times when they are encountered. ColdFusion checks the scope of variables in a certain order to find the variable, and the best practice is to use the dot notation to specify the scope of a variable. For example, for a variable MyVariable in the context of a Form to be used outside the form, use Form.MyVariable to access the variable. This is very similar to JavaScript's access to control fields on a form. Both Form.MyVariable and Form["MyVariable"] are accepted notations to access MyVariable.
- Variables; local
- Form
- URL
- Attributes
- Caller
- This Tag
- Request
- CGI
- Cookie
- Client
- Session
- Application
- Server
- Flash
- Arguments
- This
- Function Call
Here is a CFM file that shows examples of two of the scopes from the above list, Variables and Server. Server has a set of built in variables which can be accessed as shown.
<!--- Scope: Variables and Server scope--->
<cfscript>
WriteOutput("Version of ColdFusion used in this tutorial is:<br/> ");
WriteOutput(Server.ColdFusion.ProductVersion);
WriteOutput("<p></p>");
tutor="Jay";
WriteOutput("The author of this tutorial is: <br/>");
WriteOutput(Variables.tutor);
</cfscript>
The server processed file in the browser is shown in this picture
Next: Data Type Category of Variables >>
More ColdFusion Articles
More By Jayaram Krishnaswamy