PHPackages                             mossadal/math-parser - 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. mossadal/math-parser

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

mossadal/math-parser
====================

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

v1.3.16(7y ago)1791.7M—6.4%42[9 issues](https://github.com/mossadal/math-parser/issues)[3 PRs](https://github.com/mossadal/math-parser/pulls)13LGPL-3.0PHPPHP &gt;=5.4.0CI failing

Since Jan 4Pushed 4y ago7 watchersCompare

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

READMEChangelogDependencies (3)Versions (24)Used By (13)

math-parser
===========

[](#math-parser)

[![Latest Stable Version](https://camo.githubusercontent.com/d553704314d312e18c10b6bf8f0443ee796f25c79e09e755592a53429ac38c25/68747470733a2f2f706f7365722e707567782e6f72672f6d6f73736164616c2f6d6174682d7061727365722f762f737461626c65)](https://packagist.org/packages/mossadal/math-parser) [![Total Downloads](https://camo.githubusercontent.com/54c577cc5efa111f91797ae3a2f72118bec7ceef3bc4aa3e5e8b0cc572a31337/68747470733a2f2f706f7365722e707567782e6f72672f6d6f73736164616c2f6d6174682d7061727365722f646f776e6c6f616473)](https://packagist.org/packages/mossadal/math-parser) [![License](https://camo.githubusercontent.com/ee9bbd3bbc1e8b31eff6e911acd23a2976797fdea75f1ea4d77fad14e5d19df0/68747470733a2f2f706f7365722e707567782e6f72672f6d6f73736164616c2f6d6174682d7061727365722f6c6963656e7365)](https://packagist.org/packages/mossadal/math-parser)[![Code Climate](https://camo.githubusercontent.com/af06dd1126d53246554ac5ddaf7c2d487a86b6b00bee9bf3ce3476f090c5d48e/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d6f73736164616c2f6d6174682d7061727365722f6261646765732f6770612e737667)](https://codeclimate.com/github/mossadal/math-parser)

DESCRIPTION
-----------

[](#description)

PHP parser and evaluator library for mathematical expressions.

Intended use: safe and reasonably efficient evaluation of user submitted formulas. The library supports basic arithmetic and elementary functions, as well as variables and extra functions.

The lexer and parser produces an abstract syntax tree (AST) that can be traversed using a tree interpreter. The math-parser library ships with three interpreters:

- an evaluator computing the value of the given expression.
- a differentiator transforming the AST into a (somewhat) simplied AST representing the derivative of the supplied expression.
- a rudimentary LaTeX output generator, useful for pretty printing expressions using MathJax

EXAMPLES
--------

[](#examples)

It is possible to fine-tune the lexer and parser, but the library ships with a StdMathParser class, capable of tokenizing and parsing standard mathematical expressions, including arithmetical operations as well as elementary functions.

```
use MathParser\StdMathParser;
use MathParser\Interpreting\Evaluator;

$parser = new StdMathParser();

// Generate an abstract syntax tree
$AST = $parser->parse('1+2');

// Do something with the AST, e.g. evaluate the expression:
$evaluator = new Evaluator();

$value = $AST->accept($evaluator);
echo $value;

```

More interesting example, containing variables:

```
$AST = $parser->parse('x+sqrt(y)');

$evaluator->setVariables([ 'x' => 2, 'y' => 3 ]);
$value = $AST->accept($evaluator);

```

We can do other things with the AST. The library ships with a differentiator, computing the (symbolic) derivative with respect to a given variable.

```
use MathParser\Interpreting\Differentiator;

$differentiator = new Differentiator('x');
$f = $parser->parse('exp(2*x)-x*y');
$df = $f->accept($differentiator);

// $df now contains the AST of '2*exp(x)-y' and can be evaluated further
$evaluator->setVariables([ 'x' => 1, 'y' => 2 ]);
$df->accept($evaluator);

```

### Implicit multiplication

[](#implicit-multiplication)

Another helpful feature is that the parser understands implicit multiplication. An expression as `2x` is parsed the same as `2*x` and `xsin(x)cos(x)^2` is parsed as `x*sin(x)*cos(x)^2`.

Note that implicit multiplication has the same precedence as explicit multiplication. In particular, `xy^2z` is parsed as `x*y^2*z` and **not** as `x*y^(2*z)`.

To make full use of implicit multiplication, the standard lexer only allows one-letter variables. (Otherwise, we wouldn't know if `xy` should be parsed as `x*y` or as the single variable `xy`).

DOCUMENTATION
-------------

[](#documentation)

For complete documentation, see the [github.io project page](http://mossadal.github.io/math-parser/index.html)

THANKS
------

[](#thanks)

The Lexer is based on the lexer described by Marc-Oliver Fiset in his [blog](http://marcofiset.com/programming-language-implementation-part-1-lexer/).

The parser is a version of the "Shunting yard" algorithm, described for example by [Theodore Norvell](http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#shunting_yard).

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity59

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 83.7% 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

Every ~47 days

Recently: every ~87 days

Total

22

Last Release

2802d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/39afaf0ed3bbfc03435a01af7c3dffa69967a91efe8bbcfa4c0878edbc9aad63?d=identicon)[mossadal](/maintainers/mossadal)

---

Top Contributors

[![mossadal](https://avatars.githubusercontent.com/u/8174070?v=4)](https://github.com/mossadal "mossadal (87 commits)")[![TonisOrmisson](https://avatars.githubusercontent.com/u/6357451?v=4)](https://github.com/TonisOrmisson "TonisOrmisson (13 commits)")[![Balus1](https://avatars.githubusercontent.com/u/5885834?v=4)](https://github.com/Balus1 "Balus1 (1 commits)")[![byCedric](https://avatars.githubusercontent.com/u/1203991?v=4)](https://github.com/byCedric "byCedric (1 commits)")[![HorseLuke](https://avatars.githubusercontent.com/u/1235046?v=4)](https://github.com/HorseLuke "HorseLuke (1 commits)")[![nticaric](https://avatars.githubusercontent.com/u/824840?v=4)](https://github.com/nticaric "nticaric (1 commits)")

---

Tags

parsermathematics

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mossadal-math-parser/health.svg)

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

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M732](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

3.0k404.0M702](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)

PHPackages © 2026

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