这是我创建的,请查看:Formula Interpreter
它是如何工作的?
首先,使用公式及其参数创建FormulaInterpreter 的实例
$formulaInterpreter = new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]);
使用execute() 方法解释公式。它将返回结果:
echo $formulaInterpreter->execute();
一行
echo (new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]))->execute();
示例
# Formula: speed = distance / time
$speed = (new FormulaInterpreter("distance/time", ["distance" => 338, "time" => 5]))->execute() ;
echo $speed;
#Venezuela night overtime (ordinary_work_day in hours): (normal_salary * days_in_a_work_month)/ordinary_work_day
$parameters = ["normal_salary" => 21000, "days_in_a_work_month" => 30, "ordinary_work_day" => 8];
$venezuelaLOTTTArt118NightOvertime = (new FormulaInterpreter("(normal_salary/days_in_a_work_month)/ordinary_work_day", $parameters))->execute();
echo $venezuelaLOTTTArt118NightOvertime;
#cicle area
$cicleArea = (new FormulaInterpreter("3.1416*(radio*radio)", ["radio" => 10]))->execute();
echo $cicleArea;
关于公式
- 它必须至少包含两个操作数和一个运算符。
- 操作数的名称可以是大写或小写。
- 到目前为止,不包括 sin、cos、pow 等数学函数。我正在努力将它们包括在内。
- 如果您的公式无效,您将收到如下错误消息:错误,您的公式 (single_variable) 无效。
- 参数的值必须是数字。
如果你愿意,你可以改进它!