Understanding Variables and Expressions


Simple in concept, ColdFusion variables are key to building dynamic Web pages. They provide placeholders in which to put different values. ColdFusion variables are used in expressions. Expressions are generally used in two ways: inside a <cfoutput> block to build output on a Web page, and to the right of equals signs (=) in <cfset> statements.

NOTE

Remember the following rules when naming ColdFusion variables:

  • Names must contain letters, numbers, and the underscore characters only.

  • Each name must start with a letter.

  • Spaces and any special characters besides the underscore are not allowed.


But what exactly is an expression? The ColdFusion documentation says that expressions are "language constructs that allow you to create sophisticated applications." The best way to define an expression is to say it is simply a combination of the following:

  • Operands such as integers, strings, real numbers, arrays, query results, and variables

  • Operators such as +, MOD, and AND

  • Functions that return changed data such as UCase() and DateFormat()

In other words, the following is an expression:

 #var# 

as is

 100 

Expressions can also include calculations, like this:

 var2-var1 

Expressions may be used with CFML tags as well as in output, so this is valid:

 <cfset result=var2-var1> 

as is

 <cfoutput>#var2-var1#</cfoutput> 

TIP

ColdFusion variable names can be variables themselves. If you want to assign a value to a variable, simply use <cfset> with the variable name to the left of the = and quotation marks and #s as follows:

 <cfset "#TheVar#"=15> 




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