Parse math operations with PHP

前端 未结 6 1910
执笔经年
执笔经年 2020-12-01 22:14

I am working on a project where I need to make a function that will parse the 4 default math operations (addition, subtraction, multiplication, division). It would be nice i

6条回答
  •  青春惊慌失措
    2020-12-01 22:56

    You could use eval() (WARNING: be sure what enters is a math operation and not some other arbitrary input or php code).

    $input = "3 + (4 - 2 * 8) / 2";
    
    eval('$result = ' . $input . ';');
    
    echo "The result is $result";
    

提交回复
热议问题