0

I am trying to find out a way to do maths dynamically. I want to be able to add a formula to MySQL and then solve it in PHP and display the answer.

So I'll save it like getItemPrice(10) * getItemPrice(50) However I'm unsure how to execute functions in PHP if its saved like that.

I thought about splitting the date but I want to be able to do more advanced formulas then that. I have no idea how to do this to be honest...

jrbedard
  • 3,662
  • 5
  • 30
  • 34
JnDoOl
  • 1
  • 1
  • Possible duplicate of [How to evaluate formula passed as string in PHP?](http://stackoverflow.com/questions/1015242/how-to-evaluate-formula-passed-as-string-in-php) – HPierce Jan 14 '17 at 16:54

1 Answers1

0

If the text you stored in db is a valid php code you can use

eval — Evaluate a string as PHP code

http://php.net/manual/en/function.eval.php

otherwise if the text is a valid mysql code you can generate a dinamic sql statementes and the execute as a normal sql statemets ..

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Be advised that you *must absolutely* be sure you can trust the data in the db (eg. not user-generated-content) when using this approach since it's code that gets directly executed on your page. – ccKep Jan 14 '17 at 17:25