Download the PHP package nxp/math-executor without Composer
On this page you can find all versions of the php package nxp/math-executor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nxp/math-executor
More information about nxp/math-executor
Files in nxp/math-executor
Package math-executor
Short Description Simple math expressions calculator
License MIT
Homepage http://github.com/NeonXP/MathExecutor
Informations about the package math-executor
MathExecutor
A simple and extensible math expressions calculator
Features:
- Built in support for +, -, *, /, % and power (^) operators
- Parentheses () and arrays [] are fully supported
- Logical operators (==, !=, <, <, >=, <=, &&, ||, !)
- Built in support for most PHP math functions
- Support for BCMath Arbitrary Precision Math
- Support for variable number of function parameters and optional function parameters
- Conditional If logic
- Support for user defined operators
- Support for user defined functions
- Support for math on user defined objects
- Dynamic variable resolution (delayed computation)
- Unlimited variable name lengths
- String support, as function parameters or as evaluated as a number by PHP
- Exceptions on divide by zero, or treat as zero
- Unary Plus and Minus (e.g. +3 or -sin(12))
- Pi ($pi) and Euler's number ($e) support to 11 decimal places
- Easily extensible
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 Support
By default, MathExecutor
uses PHP floating point math, but if you need a fixed precision, call useBCMath(). Precision defaults to 2 decimal points, or pass the required number.
WARNING
: Functions may return a PHP floating point number. By doing the basic math functions on the results, you will get back a fixed number of decimal points. Use a plus sign in front of any stand alone function to return the proper number of decimal places.
Division By Zero Support:
Division by zero throws a \NXP\Exception\DivisionByZeroException
by default
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 MathExecutor
You 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 '\+'.
Documentation
Full class documentation via PHPFUI/InstaDoc
Future Enhancements
This package will continue to track currently supported versions of PHP.