Evaluate Math Prob. String or convert a Math Prob int to a string

I want to do the following
[php]

    $prob = "2+2";
     
      echo $problem;

      echo "Answer is".$prob;
                                 //I need to convert the prob to the answer.

             
      //OR 

     $prob = 2+2;

       echo $prob;
             //Answer needs to be converted to "2+2" , without the quotes.


     echo "Answer is".$prob;

[/php]

Well, this can be done. Perhaps with the EVAL() function.

Here is a link that might help. Look down the page a little and you will see where they are testing with 22pi or something similar. In that code sample you will see how to use it.

The problem with EVAL() is that it passes what could be inputted by a user. This could cause the EVAL to pass a virus or a tricky program. So, if you are having someone type in the math function you have to be very careful with the eval function. (And, basically block viruses or code inputs.)

Hope that helps…

The problem with that is that EVAL is deprecated.

Well, I know it’s not suggested to use it in sites. But, this is due to USER inputs. Your sample wasn’t clear if you or USERS were creating the inputs. You can use it in your own pages if you realize you have to clean up any user inputs that might feed the EVAL(). Here is an interesting blog that talks about this subject a lot. Sorry, but, I couldn’t find any simple way to do it another way. Let us know if you solve it.


http://www.sitepoint.com/eval-is-dead-long-live-eval/

Sponsor our Newsletter | Privacy Policy | Terms of Service