About the Evaluate function

The Evaluate function takes one or more string expressions, dynamically evaluates their contents as expressions from left to right, and returns the result of evaluating the rightmost argument.

The following example shows the Evaluate function and how it works with ColdFusion variable processing:

<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>

Reviewing the code

The following table describes how ColdFusion processes this code:

Code

Description

<cfset myVar2="myVar">

<cfset myVar="27/9">

Sets the two variables to the following strings:

myVar

27/9

<cfoutput>

#myVar2#<br/>

#myVar#<br/>

Displays the values assigned to the variables, myVar and 27/9, respectively.

#Evaluate("myVar2")#<br>

Passes the string "myvar2" (without the quotation marks) 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 quotation marks).

#Evaluate("myVar")#<br>

Passes the string "myvar" (without the quotation marks) 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 quotation marks).

#Evaluate(myVar2)#<br>

Evaluates the variable myVar2 as the string "myVar" and passes the string (without the quotation marks) to the Evaluate function. The rest of the processing is the same as in the previous line.

#Evaluate(myVar)#<br/>

</cfoutput>

Evaluates the variable myVar as the string "27/9" (without the quotation marks), 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

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.