Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The Evalute() Function

The Evalute() Function

by  pcorreia  Posted    (Edited  )
Consider this example:
Code:
   <cfset myVar = "foo">
   <cfset myVarName = "myVar">
Obviously, one way to get the value of myVar is #myVar#. But if I don't know what the name of the variable is going to be (which is sometimes the case with query or form variables), I can't do that. I know that the name of the variable is stored in the variable #myVarName#, so I need a way to say to the ColdFusion engine, "Take the value of the variable 'myVarName', then evaluate that as the name of another variable". Since ColdFusion doesn't speak english, exactly, we have the Evaluate() function to tell it to do that. One way to accomplish the above example would be:
Code:
   #Evaluate("#myVarName#")#
Which says (from inside out):
[ol][li]Extract the value of the variable myVarName (which is a string).[/li]
[li]Treat that string like it was the name of a variable, and get the value of that variable.[/li][/ol]
In ColdFusion documentation, this is known as Dynamic Evaluation. There's a simpler way to write the Evaluate() function, which takes advantage of the fact that anywhere a function or expression expects a string in quotes, it will take an unquoted variable name instead:
Code:
   #Evaluate(myVarName)#
So this evaluates not the string "myVarName", (which in this example evaluates to "myVar"), but the value of the variable "myVarName" ("myVar", which evaluates to "foo").

Hope this helps out.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top