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

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

jstudz81/math-parser
====================

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

v1.3.13(8y ago)0480LGPL-3.0PHPPHP &gt;=5.4.0

Since Jan 4Pushed 7y ago1 watchersCompare

[ Source](https://github.com/JStudz81/math-parser-fix)[ Packagist](https://packagist.org/packages/jstudz81/math-parser)[ Docs](https://github.com/mossadal/math-parser)[ RSS](/packages/jstudz81-math-parser/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (22)Used By (0)

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 interepreter. 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 aritmethical 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

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 89.5% 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 ~38 days

Recently: every ~79 days

Total

20

Last Release

3053d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c80ef16dba2e28cee1efbb9c965cbd328d29aae7acd44778e1f34b69b6ba5547?d=identicon)[JStudz81](/maintainers/JStudz81)

---

Top Contributors

[![mossadal](https://avatars.githubusercontent.com/u/8174070?v=4)](https://github.com/mossadal "mossadal (77 commits)")[![JStudz81](https://avatars.githubusercontent.com/u/16603440?v=4)](https://github.com/JStudz81 "JStudz81 (7 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)")

---

Tags

parsermathematics

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/jstudz81-math-parser/health.svg)](https://phpackages.com/packages/jstudz81-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)

2.9k404.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)
