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

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

raykan/php-formula-interpreter
==============================

Fork of mormat/php-formula-interpreter with improvements

v2.0.2(3mo ago)039↑16.7%GPL-2.0-or-laterPHPPHP &gt;=8.1

Since Feb 3Pushed 3mo agoCompare

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

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

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

[](#php-formula-interpreter)

A stand-alone php library for interpreting formulas

Installation via composer
-------------------------

[](#installation-via-composer)

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

Usage
-----

[](#usage)

First, create an instance of `\Mormat\FormulaInterpreter\Compiler`

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

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

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

Finally run the formula from the executable :

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

### Examples of formulas

[](#examples-of-formulas)

```
// variables can be used
price + 2

// parenthesis can be used
(1 + 2) * 3

// default functions are available
sqrt(4)

// complex formulas can be used
(((100 * 0.43075) * 1.1 * 1.5) / (1-0.425)) * 1.105

// string are supported
lowercase('FOO')

// arrays are supported
count([2, 3, 4])

// custom functions can be registered
your_function_here(2)

// use the `in` operator to check if an item is in array
1 in [1, 2, 3]  // returns true

// use the `in` operator to check if a substring is in a string
'Wars' in 'Star Wars'

// using logical operators (`and`, `or`, `not`)
1 < x and x < 10
1 < x or x < 10
not x > 0

```

Supported types in formulas
---------------------------

[](#supported-types-in-formulas)

### Numeric values

[](#numeric-values)

A numeric value can be an integer or a float

```
    2       // integer
    2.30    // float

```

### String values

[](#string-values)

Use simple quote to delimiter strings

```
    'foobar'

```

### Array values

[](#array-values)

Use comma to separate items and brackets to wrap the items

```
    [1, 2, 3]

```

Functions, strings and operations can be used as an item of an array

```
    [cos(0), 'foobar', 2 + 2]

```

Using operators
---------------

[](#using-operators)

The following operators are available :

operatorusagedescription`+`a + bSum of a and b.`-`a - bDifference of a and b.`*`a \* bProduct of a and b.`/`a / bQuotient of a and b.`in`a in bIf a is an array, checks if b is an item of a. If a is a string, checks if b is a substring of a`and` a and bReturns `true` if both the operands are true`or` a or bReturns `true` if either of the operands is true`not` not aReturns `true` if the operand is falseThe operators `*`, `\` are being evaluated first, then the operators `+` and `-`

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)

```

Using variables
---------------

[](#using-variables)

A variable is just a word inside your formula like this :

```
price * rate / 100

```

Just before executing a formula in PHP, make sure to inject all the required variables in an array

```
$variables = array(
   'price' => 40.2,
   'rate' => 12.8
);

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

Using functions
---------------

[](#using-functions)

### Availables functions

[](#availables-functions)

nameallowed typesdescription**pi**Get value of pi**cos**`numeric`Cosine**sin**`numeric`Sine**sqrt**`numeric`Square root**pow**`numeric`,`numeric`Exponential expression**modulo**`numeric`,`numeric`Remainder of first value divided by second value**lowercase**`string`Converts to a string lowercase**uppercase**`string`Converts to a string uppercase**capitalize**`string`Make a string's first character uppercase**count**`string|array`If value is an array, count the items in the array. If value is a string, count the characters in the string### How to register a custom function ?

[](#how-to-register-a-custom-function-)

Use the `registerCustomFunction()` method in the `\Mormat\FormulaInterpreter\Compiler` class.

The custom function must implement the `\Mormat\FormulaInterpreter\Functions\FunctionInterface`. This interface contains the methods below :

- **getName()** returns the name of the function
- **supports($arguments)** returns true if the $arguments send to the function are valid.
- **execute($arguments)** executes the function and returns the value.

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.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance80

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.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

Unknown

Total

1

Last Release

105d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a1b3df3235ce0a35f39fdcd3c63cb8eb94653491c8d3eeb337f5cb572f87d3e?d=identicon)[dmuy-raykan](/maintainers/dmuy-raykan)

---

Top Contributors

[![mormat](https://avatars.githubusercontent.com/u/128204418?v=4)](https://github.com/mormat "mormat (35 commits)")[![dmuy-raykan](https://avatars.githubusercontent.com/u/150678832?v=4)](https://github.com/dmuy-raykan "dmuy-raykan (2 commits)")

---

Tags

phpformula

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/raykan-php-formula-interpreter/health.svg)](https://phpackages.com/packages/raykan-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)
