muqsit/arithmexp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

muqsit/arithmexp

最新稳定版本:1.0.1

Composer 安装命令:

composer require muqsit/arithmexp

包简介

Mathematical expression parser and evaluator for PHP

README 文档

README

CI Latest Release

arithmexp is a powerful mathematical expression parser and evaluator library for PHP with support for variable substitution, constant declaration, deterministic and non-deterministic function registration, and more.

Tip

Try out arithmexp parser on the demo site!

Installation with composer

composer require muqsit/arithmexp

Evaluating a mathematical expression

To evaluate a mathematical expression, create a Parser and invoke Parser::parse() with an expression string to obtain a reusable Expression object. Expression::evaluate() returns the value of the expression.

$parser = Parser::createDefault();
$expression = $parser->parse("2 + 3");
var_dump($expression->evaluate()); // int(5)

$expression = $parser->parse("mt_rand()");
var_dump($expression->evaluate()); // int(1370501507)
var_dump($expression->evaluate()); // int(1522981420)

Variables may be substituted at evaluation-time by passing an array<string, int|float|bool> value to Expression::evaluate().

$expression = $parser->parse("x + y");
var_dump($expression->evaluate(["x" => 2, "y" => 3])); // int(5)
var_dump($expression->evaluate(["x" => 1.5, "y" => 1.5])); // float(3)

$expression = $parser->parse("a > b or c");
var_dump($expression->evaluate(["a" => 1, "b" => 2, "c" => true])); // bool(true)

The return value type of the evaluation is consistent with that of PHP's—int + int returns an int value, while float + int|float returns a float value. bool + bool returns an int value, while int || int returns a bool value.

Note

Check out the wiki for documentation notes and further implementation details.

统计信息

  • 总下载量: 699
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2022-09-24