PHPackages                             sufir/php-calc - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. sufir/php-calc

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sufir/php-calc
==============

Simple calculation expressions

1.0.3(10y ago)27MITPHPPHP &gt;=5.6

Since Dec 27Pushed 10y ago2 watchersCompare

[ Source](https://github.com/Sufir/php-calc)[ Packagist](https://packagist.org/packages/sufir/php-calc)[ Docs](https://github.com/Sufir/php-calc)[ RSS](/packages/sufir-php-calc/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

sufir/php-calc
==============

[](#sufirphp-calc)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ed8389a5c4d2ab5705c66b81c3ab607fef850a7204609d26d52f9273527f052b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f53756669722f7068702d63616c632e737667)](https://packagist.org/packages/Sufir/php-calc)[![Build Status](https://camo.githubusercontent.com/3fac7a0ff294b68b813cb2d0d5a060acf3778d3384126099ee5cb9d734e7586c/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f53756669722f7068702d63616c632f6d61737465722e737667)](https://travis-ci.org/Sufir/php-calc)[![Quality Score](https://camo.githubusercontent.com/0ac4fb33dd39e14b47dd043264efb5c8a9f52642dee59ef15d4ff1e23b567930/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f53756669722f7068702d63616c632e737667)](https://scrutinizer-ci.com/g/Sufir/php-calc)[![Coverage Status](https://camo.githubusercontent.com/c04dea8b9d9e6c75d333deb2f4716f82bafe8bbc4c6b5cd906331e9b92b6b948/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f53756669722f7068702d63616c632e737667)](https://scrutinizer-ci.com/g/Sufir/php-calc/code-structure)[![Issues](https://camo.githubusercontent.com/6e15a0e155adb036c502506cf2d6a98c64d3e07e3cfb7e32ddedfa66ebe216df/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f53756669722f7068702d63616c632e737667)](https://github.com/Sufir/php-calc/issues)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)

Install
-------

[](#install)

Via Composer

```
$ composer require sufir/php-calc
```

Usage
-----

[](#usage)

```
$lexer = new Lexer();
$converter = new Converter();
$calc = new Calc(20);
```

### D&amp;D d20 check example:

[](#dd-d20-check-example)

```
$calc->registerFunction('d20', function () {
    return rand(1, 20);
});

$charAbilities = ['ability' => 15, /*... etc */];
$difficultyClass = 12;
$checkExpr = 'd20() + ($ability / 2 – 5)';

$tokens = $lexer->parse($checkExpr);
$result = $calc->evaluate(
    $converter->converToPostfix($tokens),
    $charAbilities
);

// simple d20 ability check
if ($result < $difficultyClass) {
    echo 'You fail!!!';
} else {
    echo 'Congratulation!';
}
```

### Area of a circle example:

[](#area-of-a-circle-example)

```
$expr = '$Pi*$r^2';
$radiusList = [5, 10, 15, 25, 50];
$calc->defineVar('$Pi', '3.14159265358979323846');
$tokens = $lexer->parse($expr);

foreach ($radiusList as $radius) {
    echo 'Pi * ', $radius , '^2 = ',
    $calc->evaluate(
        $converter->converToPostfix($tokens),
        ['r' => $radius]
    ),
    "\n";
}

/*
Pi * 5^2  = 78.53981633974483096150
Pi * 10^2 = 314.15926535897932384600
Pi * 15^2 = 706.85834705770347865350
Pi * 25^2 = 1963.49540849362077403750
Pi * 50^2 = 7853.98163397448309615000
*/
```

### Normal (Gaussian) distribution example:

[](#normal-gaussian-distribution-example)

```
$expr = 'normal_distribution(1, $s, $m)';

$calc->defineVar('$s', 1);
$calc->defineVar('$m', 0);
$calc->registerFunction('normal_distribution', function ($x, $σ = 1, $μ = 0) {
    $Pi = '3.1415926536';
    $p = bcmul(
        bcdiv(
            1,
            bcmul($σ, pow(bcmul(2, $Pi), 0.5))
        ),
        exp(
            -bcdiv(
                pow(bcsub($x, $μ), 2),
                bcmul(2, pow($σ, 2))
            )
        )
    );

    return $p;
});

$tokens = $lexer->parse($expr);
$nd = $calc->evaluate($tokens);
```

Testing
-------

[](#testing)

```
$ composer test
```

Credits
-------

[](#credits)

- [Sufir](https://github.com/Sufir)

License
-------

[](#license)

The MIT License (MIT).

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 75% 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 ~2 days

Total

4

Last Release

3831d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/538704?v=4)[Sufir](/maintainers/Sufir)[@Sufir](https://github.com/Sufir)

---

Top Contributors

[![Sufir](https://avatars.githubusercontent.com/u/538704?v=4)](https://github.com/Sufir "Sufir (3 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

expressionscalculationEvaluation

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/sufir-php-calc/health.svg)

```
[![Health](https://phpackages.com/badges/sufir-php-calc/health.svg)](https://phpackages.com/packages/sufir-php-calc)
```

###  Alternatives

[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k19.8M68](/packages/spatie-regex)[mjaschen/phpgeo

Simple Yet Powerful Geo Library

1.6k9.3M28](/packages/mjaschen-phpgeo)

PHPackages © 2026

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