Variables and Type


ColdFusion variables are typeless. Unlike many other computer languages, ColdFusion does not require that you tell it what type of data will be stored in the variable before it is used. So how does ColdFusion know what type of data a variable holds? The decision is made when you use the variable in an expression.

First, consider the assignment statement below:

 <cfset TheVar="10"> 

This statement sets a typeless variable equal to 10. Is it the number 10 or the string 10? It does not matter at this point.

Now look at the variable in another assignment statement below:

 <cfset TheNumber=TheVar+5> 

The arithmetic operator + is used here. It dynamically casts the type of TheVar to be a number. TheNumber is set to equal the number 15.

Now consider the variable again in another assignment, this time concatenating strings:

 <cfset TheString="The number is " & TheVar> 

This time a variable called TheString is set equal to the text "The number is " and then concatenated with TheVar. This assignment dynamically sets the type of TheVar to be a string. TheString is set equal to "The number is 10".

NOTE

ColdFusion variables are typeless when created. The type is cast when the variables are later used with an operator.


Although ColdFusion expressions are typeless, not all variables in a ColdFusion template can be used as such. When writing SQL, you must surround strings with single quotes. For more information on using SQL with ColdFusion, see Chapter 7, "Using Databases."




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