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

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

227690008/formula-parser
========================

Parsing and evaluating mathematical formulas given as strings.

v1.2.0(3y ago)015MITPHPPHP &gt;=7.3.0

Since Oct 24Pushed 3y ago1 watchersCompare

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

READMEChangelog (2)DependenciesVersions (3)Used By (0)

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

[](#formula-parser)

测试及自用，增加部分函数，请勿使用。 Test and self use, small parts and functions are added, please do not use. From：denissimon/formula-parser

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, floor, ceil
- Unlimited nested parentheses
- NaN (Not a Number)

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

[](#installation)

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

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

```
composer require 227690008/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

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 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

Every ~30 days

Total

2

Last Release

1271d ago

PHP version history (2 changes)v1.1.0PHP &gt;=7.4.0

v1.2.0PHP &gt;=7.3.0

### Community

Maintainers

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

---

Top Contributors

[![227690008](https://avatars.githubusercontent.com/u/40167198?v=4)](https://github.com/227690008 "227690008 (7 commits)")

---

Tags

parsermathexpressionparsingformulaEvaluation

### Embed Badge

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

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

###  Alternatives

[denissimon/formula-parser

Parsing and evaluating mathematical formulas given as strings.

81306.8k3](/packages/denissimon-formula-parser)[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)

PHPackages © 2026

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