Author Topic: Evaluate Math Prob. String or convert a Math Prob int to a string  (Read 144 times)

MattNow14

  • New Member
  • *
  • Posts: 19
  • Karma: +0/-0
    • View Profile
I want to do the following
PHP Code: [Select]

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



                     


ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1599
  • Karma: +26/-2
    • View Profile
Re: Evaluate Math Prob. String or convert a Math Prob int to a string
« Reply #1 on: February 07, 2012, 02:40:33 AM »
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 2*2*pi 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...

MattNow14

  • New Member
  • *
  • Posts: 19
  • Karma: +0/-0
    • View Profile
Re: Evaluate Math Prob. String or convert a Math Prob int to a string
« Reply #2 on: February 12, 2012, 11:57:55 PM »
The problem with that is that EVAL is deprecated.

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1599
  • Karma: +26/-2
    • View Profile
Re: Evaluate Math Prob. String or convert a Math Prob int to a string
« Reply #3 on: February 13, 2012, 06:49:58 PM »
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/