1

I am dealing with a problem. I have formulas for various calculations and they all are strings in JSON objects. I need to apply variables from PHP and calculate the value using these formulas. Is there any way of achieving this? For example:

My JSON written formula (string format):

"formulas": [
    {
      "id": "aag",
      "parameter": "",
      "exp": "((FiO2*713)-(PaCO2/0.8))-PaO2"
    },
    {
      "id": "naa",
      "parameter": "",
      "exp": "(age/4)+4"
    }
  ],

So I get the object with PHP and I can access to every element in that object. And I have inputs with variables, that are used in these formulas (FiO2, PaCO2, age and etc.). I need to somehow get this formula as a legit formula and not string ($formula = (($FiO2*713)-($PaCO2/0.8))-$PaO2;). Is it possible? Thanks in advance :)

z.siddy
  • 11
  • 2
  • Don't use the `eval()`. It is bad.You will most likely need to find a class that can handle math operations from a string input. Once you have that swapping out the key terms with the variable values is easy enough with the `str_replace()` function. Good luck. – Joseph_J Aug 08 '18 at 23:36

0 Answers0