nickfan/math-expression

There is no license information available for the latest version (dev-master) of this package.

A math expression parser

dev-master 2016-12-30 00:41 UTC

This package is not auto-updated.

Last update: 2024-11-09 21:06:53 UTC


README

rewrite form https://gist.github.com/dremie/fcb1f5beecc327679de8cca51c8e4743 ( which fork form original https://gist.github.com/ircmaxell/1232629 )

Usage

Examples

use Nickfan\MathExpression\Math;

$math = new Math();
$answer = $math->evaluate('(2 + 3) * 4');
var_dump($answer);
// int(20)
$answer = $math->evaluate('1 + 2 * ((3 + 4) * 5 + 6)');
var_dump($answer);
// int(83)
$answer = $math->evaluate('(1 + 2) * (3 + 4) * (5 + 6)');
var_dump($answer);
// int(231)

$math->registerVariable('a', 4);
$answer = $math->evaluate('($a + 3) * 4');
var_dump($answer);
// int(28)
$math->registerVariable('a', 5);
$answer = $math->evaluate('($a + $a) * 4');
var_dump($answer);
// int(40)