Calculate a sum in a string

前端 未结 5 903
我寻月下人不归
我寻月下人不归 2020-12-20 05:05

I have string like this: $string = \"a + b + c\";. Now I would like to calculate the string as sum.

For example:

$a = 10;
$b = 10;
$c =          


        
5条回答
  •  情深已故
    2020-12-20 05:37

    My standard answer to this question whenever it crops up:

    Don't use eval (especially as you're stating that this is user input) or reinvent the wheel by writing your own formula parser.

    Take a look at the evalMath class on PHPClasses. It should do everything that you're asking, in a safe sandbox.

提交回复
热议问题