PHPackages                             geldek/math-expression - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. geldek/math-expression

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

geldek/math-expression
======================

PHP parser and evaluator library for mathematical expressions

00PHP

Since Aug 30Pushed 5y ago1 watchersCompare

[ Source](https://github.com/geldek/math-expression)[ Packagist](https://packagist.org/packages/geldek/math-expression)[ RSS](/packages/geldek-math-expression/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Simple expression parser and evaluator
======================================

[](#simple-expression-parser-and-evaluator)

PHP parser and evaluator library for mathematical expressions. The library implements following features:

- expression parsing and evaluation
- hexadecimal, binary and scientific notation number parsing
- variables
- using anonymous functions to resolve variable values at runtime
- executing custom php functions
- extending the Expression class for executing custom methods
- function whitelist

The library does not support functions with variable number of arguments. It is an implementation of [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm)as described by Wikipedia.

Installation
------------

[](#installation)

Use composer to install the package.

```
    composer require geldek/math-expression
```

Usage
-----

[](#usage)

Following code evaluates this expression: **(.1 + 2.9e3)^2 \* 3 / -cos(0) + 0x1F % 0b11**

```
    use geldek\math\Expression;
    use geldek\math\ExpressionException;
    use geldek\math\Token;

    $expression = new Expression('(.1 + 2.9e3)^2 * 3 / -cos(0) + 0x1F % 0b11');
    $tokens = $expression->parse();
    $result = $expression->calculate();
    var_dump($result);
```

Following code is using variables and closures to calculate the variable value when calculating the expression.

```
    use geldek\math\Expression;
    use geldek\math\ExpressionException;
    use geldek\math\Token;

    $expression = new Expression('var1 + var2 + z_1', [
        'var1' => 1,
        'var2' => function($name) {
             return $name == 'y' ? 2 : 3;
        }
    ]);
    $expression->setVariable('z_1', 5);
    $tokens = $expression->parse();
    $result = $expression->calculate();
    var_dump($result);
```

Following code is using custom php function.

```
    use geldek\math\Expression;
    use geldek\math\ExpressionException;
    use geldek\math\Token;

    function mymax($a, $b) {
        return $a > $b ? $a : $b;
    }

    $expression = new Expression('mymax(2,-3)');
    $tokens = $expression->parse();
    $result = $expression->calculate();
    var_dump($result);
```

Following code is extending the Expression class to call custom method.

```
    use geldek\math\Expression;
    use geldek\math\ExpressionException;
    use geldek\math\Token;

    class ExpressionEx extends Expression
    {
        public function __construct($exp, $vars = []) {
            parent::__construct($exp, $vars);
        }

        public function mymin($a, $b) {
            return $a > $b ? $b : $a;
        }
    }

    $expression = new ExpressionEx('mymin(2,-3)');
    $tokens = $expression->parse();
    $result = $expression->calculate();
    var_dump($result);
```

Following code handles invalid expression by catching ExpressionException. Besides the error message you can get the token value and position that caused the error.

```
    use geldek\math\Expression;
    use geldek\math\ExpressionException;
    use geldek\math\Token;

    try {
        $expression = new Expression('((1+2)+3');
        $tokens = $expression->parse();
        $result = $expression->calculate();
        var_dump($result);
    }
    catch(ExpressionException $ex) {
        $token = $ex->getToken();
        $position = $ex->getPosition();
    }
```

Following code will throw and exception, because the function in the expression is not whitelisted.

```
    use geldek\math\Expression;
    use geldek\math\ExpressionException;
    use geldek\math\Token;

    try {
        $expression = new Expression('cos(0)');
        $expression->setWhiteList(['sin']);
        $tokens = $expression->parse();
        $result = $expression->calculate();
        var_dump($result);
    }
    catch(ExpressionException $ex) {
        $token = $ex->getToken();
        $position = $ex->getPosition();
    }
```

Running tests
-------------

[](#running-tests)

If you install dev dependencies you can run the test as follows:

```
    .\vendor\bin\phpunit tests
```

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity32

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/3154fbf910d238b9c832cddbc6452bda7f57235a1b533011e5015a62f0a0d6dc?d=identicon)[geldek](/maintainers/geldek)

### Embed Badge

![Health badge](/badges/geldek-math-expression/health.svg)

```
[![Health](https://phpackages.com/badges/geldek-math-expression/health.svg)](https://phpackages.com/packages/geldek-math-expression)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
