seregas / formula
Formula
Installs: 35 222
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 3
Open Issues: 3
Requires
- php: ^7.0
Requires (Dev)
- phpunit/dbunit: 2.0.*
- phpunit/php-code-coverage: ^4
- phpunit/php-invoker: 1.1.*
- phpunit/phpunit: 5.4.*
This package is auto-updated.
Last update: 2024-12-08 14:47:48 UTC
README
This package can parse and evaluate formulas with variables.
It can take a string with a math expression and parses it so it can be evaluated replacing variables in the expression by given values.
The packages supports operations like addition, subtraction, multiplication and division.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist seregas/formula "*"
or add
"seregas/formula": "*"
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by :
$parser = new \socialist\formula\Formula('2 + 3 * 2,65 + 25 - 26'); $parser->calculate(); // 8.95
Also in the formula you can use variables:
$parser = new \socialist\formula\Formula('2 + 3 * p + 25 - 26'); $parser->setVariable('p', 2,65); $parser->calculate(); // 8.95
And insert comments like /*...*/
, [...]
or {...}
:
$parser = new \socialist\formula\Formula('2 + 3 * p /* price */ + 25 - 26'); $parser->setVariable('p', 2,65); $parser->calculate(); // 8.95