PHP: Calculate a math function f(x) in a string

前端 未结 1 608
你的背包
你的背包 2020-12-07 04:03

Is it possible to calculate a math function f(x) that in a string. Something like this:

$function = \'2x+3\';
$x = 4;
math_function($function, $x); //Shoud p         


        
相关标签:
1条回答
  • 2020-12-07 04:50

    My standard answer to this question whenever it crops up:

    Don't use eval (especially if the formula contains 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 want in a nice safe sandbox.

    0 讨论(0)
提交回复
热议问题