This question has already been answered:

Good day to all.
There is a div, I bring it in there by pressing the buttons of the number, it’s all written as a string.
How can you calculate the expression in this line, let's say.

8 * 2/5 + 2

While it comes to mind, go through the cycle and if there is any sign, perform the appropriate action enter image description here


Thank you all in advance for the tips and answers: 3

Reported as a duplicate by participants Ipatiev , user194374, Denis Bubnov , ermak0ff , Vadim Ovchinnikov 21 Feb '17 at 7:17 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Reverse Polish record?) - Moonvvell
  • I can not understand one thing: Div, buttons ... What does it have to do with PHP? - Ipatiev
  • @ Ipatiev how can I figure out what is written in diva through pkhp? I pass the values ​​through $ _POST, but xs how to calculate this line - tweeker
  • Why to transfer to PHP, to consider there, and then transfer back? what's the point of driving this unfortunate line back and forth? - Ipatiev
  • Indeed, why not count on JS? - Anton Zikov

1 answer 1

Use eval :

$ma ="8*2/5+2"; $p = eval('return '.$ma.';'); print $p; 
  • 3
    You can always be sure that they write this debilitating answer - Ipatiev
  • @ Is he wrong? - tweeker
  • eval dangerous thing ... - Anton Zikov
  • In the example of the link there are others. Ultimately, the person will decide how to use it. - Daniel Protopopov