PHPackages                             khumbal/php-formula-interpreter - 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. khumbal/php-formula-interpreter

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

khumbal/php-formula-interpreter
===============================

Formula interpreter for PHP

v1.1.1(1y ago)14862MITPHPPHP ^8.1

Since Oct 8Pushed 1y ago1 watchersCompare

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

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

php-formula-interpreter
=======================

[](#php-formula-interpreter)

A formula interpreter for php

Why this library ?
------------------

[](#why-this-library-)

Some user could wants to perform a simple calculation and being able to change it as much as he can. Before using a library, you could use the `eval` function. But this method has two major drawbacks :

- Security. A php script is being evaluated by the eval function. Php is a very powerful language, perhaps too powerful for a user especially when the user wants to inject malicious code.
- Complexity. Php is also complex for someone who doesn't understand programming language. It could be nice to interpret an excel-like formula instead.

Installing
----------

[](#installing)

Use Composer to install it:

```
composer require khumbal/php-formula-interpreter

```

How does it work ?
------------------

[](#how-does-it-work-)

First, create an instance of `FormulaInterpreter\Compiler`

```
$compiler = new FormulaInterpreter\Compiler();
```

Then use the `compile()`method to parse the formula you want to interpret. It will return an instance of `FormulaInterpreter\Executable` :

```
$executable = $compiler->compile('2 + 2');
```

Finally run the formula from the executable :

```
$result = $executable->run();
// $result equals 4
```

Using operators
===============

[](#using-operators)

Operator multiplication (\*) and division () are being evaluted first, then addition (+) and subtraction (-)

You can also force the prioriry of an expression by using parentheses like this

```
'2 * (3 + 2)'
```

You can use as many parentheses as you like.

```
'2 * (2 * (3 + 2 * (3 + 2)) + 2)'
```

Comparison operators
--------------------

[](#comparison-operators)

Supported comparison operators are `=`, `!=`, ``, `>=`, `>`, ` 40.2,
   'rate' => 12.8
];

$executable->run($variables);

```

Variables now supports dot `.` as the name.

```
$variables = [
   'user.name' => 'John Doe'
   'user.age' => '23'
];

$executable->run($variables);
```

String support
--------------

[](#string-support)

Variables can contain string. Use double quote (`"`) for denote a string.

```
'a = "Hello world"'
```

Using functions
===============

[](#using-functions)

Here is an example of expression using a function :

```
   'cos(0)'
```

Available functions : `pi`, `pow`, `cos`, `sin`, `sqrt` &amp; `modulo`

You can embed functions as much as you like

```
   'pow(sqrt(4), 2)'
```

Custom functions
----------------

[](#custom-functions)

Now function can be added before compiling should you want to add more variables.

```
$compiler = new Compiler();
$compiler->registerFunction('foobar', function ($a, $b) {
    return $a + $b;
});
$executable = $compiler->compile("foobar(foo, bar)");
$params = [
    'foo' => 1,
    'bar' => 2
];
$executable->run($params);
```

Listing parameters
==================

[](#listing-parameters)

You can get a list of all variables that must be provided to the `run()` method in order to run the executable:

```
$executable = $compiler->compile('foo + bar');

print_r($executable->getParameters());
```

This will output:

```
    Array
    (
        [0] => foo
        [0] => bar
    )
```

###  Health Score

38

—

LowBetter than 84% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 55.6% 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 ~813 days

Total

5

Last Release

613d ago

PHP version history (2 changes)v1.0.1PHP ^7.0

v1.1.0PHP ^8.1

### Community

Maintainers

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

---

Top Contributors

[![petrabarus](https://avatars.githubusercontent.com/u/523289?v=4)](https://github.com/petrabarus "petrabarus (10 commits)")[![khumbal](https://avatars.githubusercontent.com/u/778014?v=4)](https://github.com/khumbal "khumbal (7 commits)")[![simon-forb](https://avatars.githubusercontent.com/u/54510453?v=4)](https://github.com/simon-forb "simon-forb (1 commits)")

---

Tags

php

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/khumbal-php-formula-interpreter/health.svg)

```
[![Health](https://phpackages.com/badges/khumbal-php-formula-interpreter/health.svg)](https://phpackages.com/packages/khumbal-php-formula-interpreter)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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