Our users can run mathematical calculations based on form fields. To evaluate the calculation, I'm using expr-eval as expression evaluator for javascript (client side) (https://github.com/silentmatt/expr-eval).
An example expression is: x = 10; 2 + (4*x)
For security reasons, we should verify the result of the calculation on server side as well. Therefore, I want to re-run the calculation with the incoming data on the server using PHP, after the form has been submitted.
How would I do that? expr-eval comes with a lot of possibilities like ternary conditions, variable declarations and so on. As there is no equivalent library for PHP, what would be the way of running such a verification? I could setup a Node.js server running the expr-eval, but would an additional request really be the best practice here?
Eval will do most of what you need, but:
i.e. you're going to need to define your syntax very strictly, implement your own parser then hand off the final expression to eval.
Something like....