3 Replies Latest reply on Dec 8, 2011 10:17 AM by reggiejackson44

    Evaluate Function

    reggiejackson44

      Hello, I was handed a coldfusion web site to work on that uses the evaluate function and I'm having trouble understanding the theory behind evaluate and what is expected to be returned.

       

      Here is the code:

      #dollarFormat(evaluate((price quantity))#

       

      I looked at the Coldfusion Documentation on evaluate but I still don't understand.

       

      Are price and quantity being multipled or operated on before evaluate?

      What exactly is evaluate doing to price and quantity?

      What is being returned? ...a number to be formated as currency?

       

      Thank you for any help or point in the right direction.

        • 1. Re: Evaluate Function
          insuractive Level 3

          The evaluate function allows you to perform dynamic evaluation of coldfusion variables.

           

          e.g.:

           

          <cfset myVar = 1>

          <cfset testVar1 = "foo">

           

          <cfoutput>

          #Evaluate("testVar" & myVar)#

          </cfoutput>

           

          Result: foo

           

          Early in CF, it was pretty much the only way that you could programatically reference variable names by combining a string with one or more CF variables.  In modern CF coding, there are very few, rare instances where evaluate() is neccessary.  Using the current Variable-Scopes-as-Structures functionality in CF, you can usually accomplish the same thing using the Struct-Key method:

           

          #VARIABLES["testVar" & myVar]#

           

          This is almost always preferable since there is a bit of security risk using Evaluate() to dynamically generate variable references.

           

          If I have interpreted your code correctly, I don't think you need the Evaluate() statement.  It seems like  you are trying to do:

           

          #DollarFormat(price*quantity)#

           

          Does that help clarify?

           

          - Michael

          1 person found this helpful
          • 2. Re: Evaluate Function
            BKBK Adobe Community Professional & MVP

            reggiejackson44 wrote:

             

            Hello, I was handed a coldfusion web site to work on that uses the evaluate function and I'm having trouble understanding the theory behind evaluate and what is expected to be returned.

             

            Here is the code:

            #dollarFormat(evaluate((price quantity))#

             

            I looked at the Coldfusion Documentation on evaluate but I still don't understand.

             

            Are price and quantity being multipled or operated on before evaluate?

            What exactly is evaluate doing to price and quantity?

            What is being returned? ...a number to be formated as currency?

             

            Check again. Is there really a space between price and quantity? I don't expect so.

             

            In any case, you're not alone in finding the evaluate function difficult to understand. I find it among the most complicated functions in ColdFusion.

             

            I'll explain its most basic functionality (it can have much more complex behaviour, typically when it is used in conjunction with the de() function). In general, it takes as function parameters a comma delimited list of string variables . It then evaluates them from left to right, holding the current result in memory.

             

            The key is that ColdFusion treats the literal content of the string as the name of the variable to be evaluated, unless that content is the final evaluated value. Take, for example

             

            <cfset x="z">

            <cfset y=2>

            <cfset result1 = evaluate("x")>

            <cfset result2 = evaluate("y")>

            <cfset result3 = evaluate(y)>

            <!--- <cfset result4 = evaluate(x)> --->

             

            Result1: The literal content of "x" is x. So, to get result1, ColdFusion will evaluate the value of the variable named x. The result is the string "z".

             

            Result2: The literal content of "y" is y. To get result2, ColdFusion will evaluate value of the variable named y. The result is 2.

             

            Result3: The literal content of y is 2. This is already the final evaluated value. So ColdFusion proceeds no further. The result is 2.

             

            Result4: The literal content of x is z. To get result4, ColdFusion will evaluate the value of the variable named z. However, there is no such variable. So that line will result in an error, telling you that the variable z is undefined.

             

            Let's now proceed to examples in which we pass multiple arguments to the evaluate functrion.

             

            <cfset x="foo">

            <cfset foo="bar">

            <cfset bar="my_value">

            <cfset foo_bar = "BKBK1">

            <cfset my_value=5>

            <cfset barmy_value="BKBK2">

             

             

            Let's discuss, for example, evaluate(x,foo,foo&bar). The literal value of x is foo. So ColdFusion evaluates the variable named foo. It then proceeds from left to right, carrying the result along in memory. That result is "bar". Next, ColdFusion encounters the argument foo, whose literal value is bar. ColdFusion therefore evaluates the variable named bar. The result is my_value.

             

            So, at this point, ColdFusion is holding the value "bar", from the variable named foo, and the value "my_value", from the variable named bar. Continuing to the right, the literal value of foo&bar is barmy_value. So ColdFusion evaluates the variable named barmy_value, hence the final result BKBK2.

             

            Here are other results:

             

            evaluate("x") = foo

            evaluate(x) = bar

             

            evaluate("foo") = bar

            evaluate(foo) = my_value

             

            evaluate("x","foo") = bar

            evaluate(x,foo) = my_value

             

            evaluate(x,foo,x&"_"&foo) = BKBK1

            evaluate(x,foo,foo&bar) = BKBK2

             

            evaluate("foo","bar") = my_value

            evaluate(foo,bar) = 5

             

            evaluate("my_value") = 5

            evaluate(my_value) = 5

            1 person found this helpful
            • 3. Re: Evaluate Function
              reggiejackson44 Level 1

              ...very confusing stuff but thank you to both replies. They did provide more insight and a better understanding of the evaluate function.