Recommend this page to a friend! |
Classes of Alexander Kiryukhin | PHP Math Expression Evaluator | README.md | Download |
|
DownloadMathExecutor ![](https://img.shields.io/badge/PHPStan-level%206-brightgreen.svg?style=flat)A simple and extensible math expressions calculatorFeatures:
Install via Composer:
Sample usage:
Functions:Default functions: * abs * acos (arccos) * acosh * arccos * arccosec * arccot * arccotan * arccsc (arccosec) * arcctg (arccot, arccotan) * arcsec * arcsin * arctan * arctg * array * asin (arcsin) * atan (atn, arctan, arctg) * atan2 * atanh * atn * avg * bindec * ceil * cos * cosec * cosec (csc) * cosh * cot * cotan * cotg * csc * ctg (cot, cotan, cotg, ctn) * ctn * decbin * dechex * decoct * deg2rad * exp * expm1 * floor * fmod * hexdec * hypot * if * intdiv * lg * ln * log (ln) * log10 (lg) * log1p * max * median * min * octdec * pi * pow * rad2deg * round * sec * sin * sinh * sqrt * tan (tn, tg) * tanh * tg * tn Add custom function to executor:
Optional parameters:
Variable number of parameters:
Operators:Default operators: Add custom operator to executor:
Logical operators:Logical operators (==, !=, <, <, >=, <=, &&, ||, !) are supported, but logically they can only return true (1) or false (0). In order to leverage them, use the built in if function:
You can think of the if function as prototyped like:
Variables:Variables can be prefixed with the dollar sign ($) for PHP compatibility, but is not required. Default variables:
You can add your own variables to executor:
Arrays are also supported (as variables, as func params or can be returned in user defined funcs):
By default, variables must be scalar values (int, float, bool or string) or array. If you would like to support another type, use setVarValidationHandler
You can dynamically define variables at run time. If a variable has a high computation cost, but might not be used, then you can define an undefined variable handler. It will only get called when the variable is used, rather than having to always set it initially.
Floating Point BCMath SupportBy default, Division By Zero Support:Division by zero throws a
Or call setDivisionByZeroIsZero
If you want another behavior, you can override division operator:
String Support:Expressions can contain double or single quoted strings that are evaluated the same way as PHP evaluates strings as numbers. You can also pass strings to functions.
To use reverse solidus character (\) in strings, or to use single quote character (') in a single quoted string, or to use double quote character (") in a double quoted string, you must prepend reverse solidus character (\).
Extending MathExecutorYou can add operators, functions and variables with the public methods in MathExecutor, but if you need to do more serious modifications to base behaviors, the easiest way to extend MathExecutor is to redefine the following methods in your derived class: * defaultOperators * defaultFunctions * defaultVars This will allow you to remove functions and operators if needed, or implement different types more simply. Also note that you can replace an existing default operator by adding a new operator with the same regular expression string. For example if you just need to redefine TokenPlus, you can just add a new operator with the same regex string, in this case '\\+'. DocumentationFull class documentation via PHPFUI/InstaDoc Future EnhancementsThis package will continue to track currently supported versions of PHP. |