Variable Scopes

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 2.  Using Variables


As we've already seen, variables can contain varying data types. Variables also can come in varying scopes. A scope is the context in which a variable exists, and it determines how long its data persists. To put it simply, a variable's scope determines where it lives, how you access it, and how long its data hangs around.

So far, we have just been using simple local variables. A local variable lives only in the template in which it was created. For example, if we use <CFSET Message="I am a local variable"> to set the value of a variable called Message in a ColdFusion template called page1.cfm, we can display the value of that variable anywhere in that template simply by using <CFOUTPUT>#Message#</CFOUTPUT>. However, if we were to attempt to use <CFOUTPUT>#Message#</CFOUTPUT> in another template called page2.cfm, ColdFusion would return an error because the Message variable does not exist in the page2.cfm template. If you want the value of a variable to persist from one template/page to another, you must use a different scope.

NOTE

The value of a local variable will also be available to any included templates that are called using <CFINCLUDE>. Because using <CFINCLUDE> is essentially the equivalent of copying and pasting code into the calling template, local variable values set on the calling template will be available for use in the code of the included page code as well.


Table 2.2 lists some of the different types of scopes we will be using throughout this book. For a complete list of scopes and their descriptions, see the "Language Reference" section of this book's web site at www.LearnColdFusionMX.com.

Table 2.2. ColdFusion Variable Scopes

Scope

Description

Variables (local)

As previously described, a local variable is only available on the page in which it was created and any included pages.

Form

This scope is for variables passed via HTML or ColdFusion forms using the post method of the form. See the section "Passing Values with Forms" later in this step.

URL

This scope contains variables passed via parameters added to the end of a URL, such as www.anysite.com?id=1175. See the section "Passing Values Via the URL" later in this step.

CGI

This scope is for environment variables that automatically accompany each page request and server response, such as browser type and server name.

Cookie

This scope is for variables used to read and write browser cookies.

Client

This scope contains variables associated with a particular client (user). They are maintained as a user moves from page to page and are available over multiple browser sessions, or visits. For more information on client variables, see Step 8, "ColdFusion Application Framework."

Session

This scope contains variables associated with one client for one session or visit. These variables time out when a user closes their browser or after periods of inactivity. For more information on session variables, see Step 8.

Application

This scope contains variables that are available throughout one entire application (web site) on a server. For more information on application variables, see Step 8.

Server

This scope contains variables that are set for a particular ColdFusion server and that are available to all applications (web sites) on that server. For more information on server variables, see Step 8.


    Team-Fly    
    Top
     



    ColdFusion MX. From Static to Dynamic in 10 Steps
    ColdFusion MX: From Static to Dynamic in 10 Steps
    ISBN: 0735712964
    EAN: 2147483647
    Year: 2002
    Pages: 140
    Authors: Barry Moore

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net