PHPackages                             choate/matex - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. choate/matex

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

choate/matex
============

PHP Mathematical expression parser and evaluator

0.0.1(3y ago)03MITPHPPHP &gt;=7.0.0

Since Nov 19Pushed 3y agoCompare

[ Source](https://github.com/Choate/matex)[ Packagist](https://packagist.org/packages/choate/matex)[ Docs](https://github.com/choate/matex)[ RSS](/packages/choate-matex/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

[![License](https://camo.githubusercontent.com/c76ec20e72bac3cdb63d274a75e9fb61ccbac47eae3ea1bac080f12210555715/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4d61646f72696e2f4d61746578)](LICENSE.md)

Matex
=====

[](#matex)

PHP Mathematical expression parser and evaluator

Features
--------

[](#features)

- Fast evaluation
- Compact codebase
- Operators: + - \* / ^ % != == &lt; &lt;= &gt; &gt;= &amp;&amp; ||
- Operators Precedence: [PHP Operators Precedence](https://www.php.net/manual/en/language.operators.precedence.php)
- Brackets, nested, unlimited levels
- Variables: predefined or estimated dynamically
- Functions: predefined or connected dynamically
- String arguments in functions, like field("name")
- String operations, currently concatenation is supported

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

[](#installation)

Using [Composer](https://getcomposer.org) run

```
$ composer require choate/matex
```

See [manual](doc/MANUAL.md) for more details and options.

Usage
-----

[](#usage)

Basic:

```
$evaluator = new \choate\matex\Evaluator();
echo $evaluator->execute('1 + 2');
```

String concatenation:

```
$evaluator = new \choate\matex\Evaluator();
echo $evaluator->execute('"String" + " " + "concatenation"');
```

Variables:

```
$evaluator = new \choate\matex\Evaluator();
$evaluator->variables = [
	'a' => 1,
	'b' => 2
	];
echo $evaluator->execute('a + b');
```

Dynamic variables:

```
public function doVariable($name, &$value) {
	switch ($name) {
		case 'b':
			$value = 2;
			break;
	}
}

$evaluator = new \choate\matex\Evaluator();
$evaluator->variables = [
	'a' => 1
	];
$evaluator->onVariable = [$this, 'doVariable'];
echo $evaluator->execute('a + b');
```

Functions:

```
static function sum($arguments) {
	$result = 0;
	foreach ($arguments as $argument)
		$result += $argument;
	return $result;
}

$evaluator = new \choate\matex\Evaluator();
$evaluator->functions = [
	'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null]
];
echo $evaluator->execute('sum(1, 2, 3)');
```

Extravaganza:

```
/*
Dynamic variable resolver
Invoked when the variable is not found in the cache
Returns the value by name
*/
public function doVariable($name, &$value) {
	switch ($name) {
		case 'zen':
			// Here may be a database request, or a function call
			$value = 999;
			break;
		case 'hit':
			$value = 666;
			break;
	}
}

/*
Dynamic function resolver
Invoked when the function is not found in the cache
Returns an associative array array with:
	ref - Function reference
	arc - Expected argument count
*/
public function doFunction($name, &$value) {
	switch ($name) {
		case 'cos':
			// Map to a system function
			$value = ['ref' => 'cos', 'arc' => 1];
			break;
		case 'minadd':
			// Map to a public object instance function
			$value = ['ref' => [$this, 'minAdd'], 'arc' => 2];
			break;
	}
}

/*
Custom functions, may be a
	- Built-in function
	- Global defined function
	- Static class function
	- Object instance function
*/
static function sum($arguments) {
	$result = 0;
	foreach ($arguments as $argument)
		$result += $argument;
	return $result;
}
// Just a sample custom function
function minAdd($a, $b) {
	$r = $a < 2 ? 2 : $a;
	return $r + $b;
}

// Let's do some calculations
$evaluator = new \choate\matex\Evaluator();
$evaluator->variables = [
	'a' => 1,
	'bet' => -10.59,
	'pi' => 3.141592653589
	];
$evaluator->onVariable = [$this, 'doVariable'];
$evaluator->functions = [
	'sin' => ['ref' => 'sin', 'arc' => 1],
	'max' => ['ref' => 'max', 'arc' => null],
	'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null]
	];
$evaluator->onFunction = [$this, 'doFunction'];
echo $evaluator->execute('a + MinAdd(PI * sin(zen), cos(-1.7 / pi)) / bet ^ ((A + 2) * 2) + sum(5, 4, max(6, hit))');
```

See [examples](examples/README.md) for code samples.

Author
------

[](#author)

Choate - [choate.yao@gmail](mailto:choate.yao@gmail) -

Dorin Marcoci -  -

License
-------

[](#license)

Matex is distributed under [MIT license](LICENSE.md).

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1275d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/398f6741841ad8a75b9b9b8f186b752608cb60cb7acfa2df41f56451af4b014f?d=identicon)[Choate](/maintainers/Choate)

---

Top Contributors

[![madorin](https://avatars.githubusercontent.com/u/11855141?v=4)](https://github.com/madorin "madorin (28 commits)")

---

Tags

parsermathexpressioncalculatorformulaevalevaluator

### Embed Badge

![Health badge](/badges/choate-matex/health.svg)

```
[![Health](https://phpackages.com/badges/choate-matex/health.svg)](https://phpackages.com/packages/choate-matex)
```

###  Alternatives

[madorin/matex

PHP Mathematical expression parser and evaluator

1161.2M1](/packages/madorin-matex)[nxp/math-executor

Simple math expressions calculator

2281.7M7](/packages/nxp-math-executor)[denissimon/formula-parser

Parsing and evaluating mathematical formulas given as strings.

81306.8k3](/packages/denissimon-formula-parser)[chriskonnertz/string-calc

StringCalc is a PHP calculator library for mathematical terms (expressions) passed as strings.

102643.3k5](/packages/chriskonnertz-string-calc)[nicoswd/php-rule-parser

Rule Engine - Rule Parser &amp; Evaluator

13078.6k7](/packages/nicoswd-php-rule-parser)

PHPackages © 2026

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