rossonero585/php-expression
Composer 安装命令:
composer require rossonero585/php-expression
包简介
Simple library for calculating math expressions
README 文档
README
Description
This library allows creating math expression using numbers, brackets, mathematical signs (+,-,*,/) and then execute them
with different arguments. It also allows adding custom functions to be executed inside expression.
It works trough eval function that receives dynamically generated string and then run it.
Example usage
Simple expression
<?php $expressionBuilder = new \Rossonero585\PhpExpression\ExpressionBuilder(); $expression = $expressionBuilder ->addArguments(['a', 'b']) ->create('a + b'); echo $expression->execute(["a" => 5, "b" => 5]); // 10 echo $expression->execute(["a" => 2, "b" => 1]); // 3
Add custom function
<?php $expressionBuilder = new \Rossonero585\PhpExpression\ExpressionBuilder(); $expression = $expressionBuilder ->addFunction('convert', function ($value, $curr1, $curr2) { // do some stuff here return 61; }) ->addArguments(["x", "curr1", "curr2"]) ->create('1.1 * convert(x, curr1, curr2) + 100'); $result = $expression->execute([ "x" => 100, "curr1" => "USD", "curr2" => "RUB" ]);
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-06