Using Local Variables


As you can see from this chapter so far, <cfset> is very helpful in assigning values to variables. If the variable already exists, <cfset> resets the value.

A common problem in Web development is using variables when you are not sure they exist. The <cfparam> tag can assist you in this situation. If the tag:

 <cfparam name="TheVar" default="Default Value"> 

did not exist, you could replace it with the following code:

 <cfif NOT IsDefined("TheVar")>  <cfset TheVar="Default Value"> </cfif> 

A lesser-known use of <cfparam> is checking the data type of a variable. The type attribute checks the data type, and if it is not of the type specified, an error is thrown. For example, if a variable is assigned using the statement:

 <cfset TheVar="Hello"> 

the following <cfparam> would generate an error:

 <cfparam name="TheVar" type="numeric" default="15"> 

The following values are valid for use with the type attribute:

  • any

  • array

  • binary

  • boolean

  • creditcard

  • date (may also be specified as usdate)

  • email

  • eurodate

  • float

  • guid

  • integer

  • numeric

  • query

  • regex (may also be specified as regular_expression)

  • ssn (may also be specified as social_security_number)

  • string

  • struct

  • telephone

  • time

  • url

  • uuid

  • variableName

  • xml

  • zipcode

Generating an error is not always a bad thing. In Chapter 26, "Error Handling," youll see how you can catch the error and programmatically deal with it.


CAUTION

When you're using <cfparam> and no prefix is used with the variable whose existence you are checking, remember that the tag looks through all scopes that do not require a prefix for a variable of that name. You may think you are checking for the existence of VARIABLES.TheVar, but if FORM.TheVar exists, <cfparam> assumes the variable exists.


Using #s

In the examples shown so far in this chapter, no prefixes have been used with <cfset> or <cfparam>. This creates a variable, called a "local" variable, whose scope is only the local page. The VARIABLES prefix is used with local variables.

NOTE

Remember that local variables can also be used in included templates.


When you use local variables, you sometimes must use the number sign (#) around them. The pair of #s basically tells ColdFusion that what is inside them must be processed, or the variable needs to be distinguished from plain text. Some common uses of local variables are displaying them within <cfoutput> tags and using them in expressions.

TIP

Because of the ambiguity over when to use #s in early versions of ColdFusion, they are sometimes overused. If you're unsure whether to use #s, remember the following rules:

  • #s are always needed when ColdFusion variables are used outside or between pairs of ColdFusion tags.

  • #s are never needed when used inside ColdFusion tags, unless the variable is inside quotation marks.


Variables and the REQUEST Scope

One limitation of local variables is that they can be used only in the page where they are created (and in any included page), but not within other code being executed (for example, Custom Tag code). To create variables that can be used by all code being processed use the REQUEST scope. Because the scope is the entire page request, the values are carried through to nested tags, such as custom tags. So instead of continually passing the same value to custom tags, you put in the REQUEST scope.

A great place to set these REQUEST scope variables is in the Application.cfc or Application.cfm files.


Youll see how to use custom tags and pass data to them in Chapter 28, "Custom Tags," and Chapter 29, "Advanced Custom Tags."


You therefore can set a variable using the REQUEST scope and then use that variable in custom tags. You create a variable in the REQUEST scope as shown in the following example:

 <cfset REQUEST.AppDSN="StoreData"> 



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

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