seregas/formula

Formula

0.3.0 2019-01-24 10:40 UTC

This package is auto-updated.

Last update: 2024-04-08 13:16:06 UTC


README

Latest Stable Version Build Status Total Downloads

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