nickfan/math-expression

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

A math expression parser

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/nickfan/math-expression

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

This package is not auto-updated.

Last update: 2025-10-12 01:39:46 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)