PHPackages                             denissimon/formula-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. denissimon/formula-parser

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

denissimon/formula-parser
=========================

Parsing and evaluating mathematical formulas given as strings.

v2.7.2(1y ago)81306.8k↓18.1%233MITPHPPHP &gt;=5.4.0

Since Aug 7Pushed 1y ago7 watchersCompare

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

READMEChangelog (8)DependenciesVersions (9)Used By (3)

Formula Parser
==============

[](#formula-parser)

[![Latest Stable Version](https://camo.githubusercontent.com/c1c42939e3a69517834afe88fa00ad4c8eb68f075e3d83d792ed7d7d3bb9d12c/68747470733a2f2f706f7365722e707567782e6f72672f64656e697373696d6f6e2f666f726d756c612d7061727365722f762f737461626c652e737667)](https://packagist.org/packages/denissimon/formula-parser)[![Total Downloads](https://camo.githubusercontent.com/289e45930da55d813e4a717828292078221544451d6e2df22bdb0669aba1bede/68747470733a2f2f706f7365722e707567782e6f72672f64656e697373696d6f6e2f666f726d756c612d7061727365722f646f776e6c6f616473)](https://packagist.org/packages/denissimon/formula-parser)[![License](https://camo.githubusercontent.com/2e91986a63763b40900f37c15c3776f1e16ffd5e9b522e0c22f5142bb3836f50/68747470733a2f2f706f7365722e707567782e6f72672f64656e697373696d6f6e2f666f726d756c612d7061727365722f6c6963656e73652e737667)](https://github.com/denissimon/formula-parser/blob/master/LICENSE)

Formula Parser is a library for parsing and evaluating mathematical formulas given as strings.

Supports:

- Operators: +, -, \*, /, ^
- Variables: x, y, z, a, b
- Numbers with decimal point '.'
- Numbers in E notation
- Constants: pi, e, Inf
- Functions: sqrt, abs, sin, cos, tan, log, exp
- Unlimited nested parentheses
- NaN (Not a Number)

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

[](#installation)

Requires [PHP 5.4 or higher](http://php.net).

To install with [Composer](https://getcomposer.org):

```
composer require denissimon/formula-parser
```

Usage
-----

[](#usage)

```
require_once __DIR__ . '/vendor/autoload.php';

use FormulaParser\FormulaParser;

$formula = '3*x^2 - 4*y + 3/y';
$precision = 2; // Number of digits after the decimal point

try {
    $parser = new FormulaParser($formula, $precision);
    $parser->setVariables(['x' => -4, 'y' => 8]);
    $result = $parser->getResult(); // [0 => 'done', 1 => 16.38]
} catch (\Exception $e) {
    echo $e->getMessage(), "\n";
}
```

The `$precision` parameter has a default of 4, and it's not required to specify:

```
$parser = new FormulaParser('3+4*2/(1-5)^8');
$result = $parser->getResult(); // [0 => 'done', 1 => 3.0001]
```

The initialized object `$parser` has the following methods:

`setValidVariables($array)` Overwrites default valid variables.

`setVariables($array)` Sets variables.

`getResult()` Returns an array \[0 =&gt; v1, 1 =&gt; v2\], where v1 is 'done' or 'error', and v2 is a computed result or validation error message, respectively.

`getFormula()` Returns the text of the formula passed to the constructor.

More usage examples can be found in `tests/FormulaParserTest.php`

License
-------

[](#license)

Licensed under the [MIT license](https://github.com/denissimon/formula-parser/blob/master/LICENSE)

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 95.1% 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 ~483 days

Total

8

Last Release

550d ago

PHP version history (2 changes)v2.3.0PHP &gt;=5.3.0

v2.4.0PHP &gt;=5.4.0

### Community

Maintainers

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

---

Top Contributors

[![denissimon](https://avatars.githubusercontent.com/u/9512458?v=4)](https://github.com/denissimon "denissimon (39 commits)")[![brazorf](https://avatars.githubusercontent.com/u/3330031?v=4)](https://github.com/brazorf "brazorf (2 commits)")

---

Tags

composerevaluationexpressionformulamathparserparsingphpparsermathexpressionparsingformulaEvaluation

### Embed Badge

![Health badge](/badges/denissimon-formula-parser/health.svg)

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

###  Alternatives

[nxp/math-executor

Simple math expressions calculator

2281.7M7](/packages/nxp-math-executor)[madorin/matex

PHP Mathematical expression parser and evaluator

1161.2M1](/packages/madorin-matex)[chriskonnertz/string-calc

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

102643.3k5](/packages/chriskonnertz-string-calc)[jakubledl/dissect

Lexing and parsing in pure PHP

2244.6M11](/packages/jakubledl-dissect)[parsica-php/parsica

The easiest way to build robust parsers in PHP.

412140.4k4](/packages/parsica-php-parsica)[jejik/mt940

An MT940 bank statement parser for PHP

911.2M2](/packages/jejik-mt940)

PHPackages © 2026

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