Reviewing the code
The following table describes how ColdFusion processes this code:
Code
<cfset myVar2="myVar">
<cfset myVar="27/9">
<cfoutput>
#myVar2#<br>
#myVar#<br>
#Evaluate("myVar2")#<br>
#Evaluate("myVar")#<br>
#Evaluate(myVar2)#<br>
#Evaluate(myVar)#<br>
</cfoutput>
As you can see, using dynamic expressions can result in substantial expression evaluation
overhead, and the code can be confusing. Therefore, you should avoid using dynamic expressions
wherever a simpler technique, such as using indexed arrays or structures can serve your purposes.
Avoiding the Evaluate function
Using the
Evaluate
The following sections provide examples of cases where you might consider using the
function.
Example 1
You might be inclined to use the
<cfoutput>1 + 1 is #Evaluate(1 + 1)#</cfoutput>
Although this code works, it is not as efficient as the following code:
<cfset Result = 1 + 1>
<cfoutput>1 + 1 is #Result#</cfoutput>
96
Chapter 4: Using Expressions and Pound Signs
Description
Sets the two variables to the following strings:
myVar
27/9
Displays the values assigned to the variables, myVar and 27/9
respectively.
Passes the string "myvar2" (without the quotes) to the Evaluate
function, which does the following:
1 Evaluates it as the variable myVar2.
2 Returns the value of the myVar2 variable, the string "myvar"
(without the quotes).
Passes the string "myvar" (without the quotes) to the Evaluate
function, which does the following:
1
Evaluates it as the variable myVar.
2 Returns the value of the myVar variable, the string "27/9"
(without the quotes).
Evaluates the variable myVar2 as the string "myVar" and passes
the string (without the quotes) to the Evaluate function. The rest
of the processing is the same as in the previous line.
Evaluates the variable myVar as the string "27/9" (without the
quotes), and passes it to the Evaluate function, which does the
following:
1
Evaluates the string as the expression 27/9
2 Performs the division.
3 Returns the resulting value, 3
function increases processing overhead, and in most cases it is not necessary.
Evaluate
function in code such as the following:
Evaluate
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers