Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A powerful mathematical expression parser and evaluator for PHP featuring variable substitution, user-defined constants, functions, deterministic functions, macros, operators, and compiler optimizations.

License

NotificationsYou must be signed in to change notification settings

Muqsit/arithmexp

Repository files navigation

CILatest Release

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

Tip

Try out arithmexp parser on thedemo site!

Installation with composer

composer require muqsit/arithmexp

Evaluating a mathematical expression

To evaluate a mathematical expression, create aParser and invokeParser::parse() with an expression string to obtain a reusableExpression 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 anarray<string, int|float|bool> value toExpression::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 anint value, whilefloat + int|float returns afloat value.bool + bool returns anint value, whileint || int returns abool value.

Note

Check out thewiki for documentation notes and further implementation details.

About

A powerful mathematical expression parser and evaluator for PHP featuring variable substitution, user-defined constants, functions, deterministic functions, macros, operators, and compiler optimizations.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp