• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Step 2. Using Variables > Variable Scopes

Variable Scopes

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.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint