PHPackages                             timo-lehnertz/formula - 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. timo-lehnertz/formula

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

timo-lehnertz/formula
=====================

An open source PHP formula parser based on https://github.com/socialist/formula

v2.2.0(5mo ago)04.0k↓50%MITPHPPHP &gt;=8.1CI failing

Since Dec 1Pushed 5mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (4)Versions (108)Used By (0)

Formula
=======

[](#formula)

[![Latest Stable Version](https://camo.githubusercontent.com/9baf3ffc7a54ef17395a9002f495d01432068341c489bbd571f79f18784808a9/68747470733a2f2f706f7365722e707567782e6f72672f74696d6f2d6c65686e6572747a2f666f726d756c612f76)](https://packagist.org/packages/timo-lehnertz/formula)[![Total Downloads](https://camo.githubusercontent.com/9bd568aa7888ec40b39430937f38961a108dc2e0b5acdc37b3259e57714dd712/68747470733a2f2f706f7365722e707567782e6f72672f74696d6f2d6c65686e6572747a2f666f726d756c612f646f776e6c6f616473)](https://packagist.org/packages/timo-lehnertz/formula)[![License](https://camo.githubusercontent.com/86f8037a36e4c41be1a3ebeff851e66eaea4c67f9d3f1d715e8ea622413f632b/68747470733a2f2f706f7365722e707567782e6f72672f74696d6f2d6c65686e6572747a2f666f726d756c612f6c6963656e7365)](https://packagist.org/packages/timo-lehnertz/formula)[![PHP Version Require](https://camo.githubusercontent.com/605cacecedf521cd6176adf02cbe91b85c7e4d0775602ece1f6096c2a01976a0/68747470733a2f2f706f7365722e707567782e6f72672f74696d6f2d6c65686e6572747a2f666f726d756c612f726571756972652f706870)](https://packagist.org/packages/timo-lehnertz/formula)[![Psalm Type Coverage](https://camo.githubusercontent.com/db1938bb086a113efd04d97401571fbb43af610f0e87aed08168f94486557c97/68747470733a2f2f73686570686572642e6465762f6769746875622f6e65726f7534322f666f726d756c612f636f7665726167652e737667)](https://packagist.org/packages/timo-lehnertz/formula)

An open source PHP formula parser based on

### Features

[](#features)

- Variables
- User defined functions
- Vectors
- Vectormath
- PHP DateTime
- PHP DateInterval

Installation
============

[](#installation)

For an installation using composer add this to you `composer.json``"require": { "timo-lehnertz/formula": "^2.0" }`

Usage
=====

[](#usage)

```
$formula = new Formula("10*a+func(b,5)");
$formula->setVariable("a", 1);
$formula->setVariable("b", 2);
$formula->setMethod("max", [$this, "func"]);
$result = formula->calculate();
```

**Table of Contents**

\[TOCM\]

\[TOC\]

Operators
---------

[](#operators)

All standart php operators are supported. `+-*/` and all logical operators. Operators respect the mathematical rules. The multiplication operator can be left out between distinct expressions.

Ternary operator
----------------

[](#ternary-operator)

Ternary operator is supported in the form of

```
a ? b : c
```

Truthy values
-------------

[](#truthy-values)

Everything that is not equal to 0 is considered a truthy value

Methods
-------

[](#methods)

You can call methods inside of a formula string and give them a definition in the in the formula object. All user defined methodsshould return something. The returned values get parsed into either a number, or a string.

Strings
-------

[](#strings)

Strings are supported in the form of `"a string"` or `'a string'`. Strings are all truthy by default and dont really serve any purpose other than beeing a parameter for functions.

Dates
-----

[](#dates)

Formula supportes DateTimeImmutable and DateInterval. To define those use a string containing a Date or string Example:

- DateTime: `"2022-11-25T23:05:47+0100"`
- DateInterval: `P1M` (interval of one month) All php date formats are supported. Check out this site for a list of DateTime formats: Here's a list for DateIntervals: Internally all dates and intervals get parsed to the UNIX timestamp for easier calculation. So if a date gets passed as parameter to a method the method will receive the timestamp and should also return a timestamp is thats what its purpose is.

Vectors
-------

[](#vectors)

Vectors are defined like `{1,2,a,b,someMethod()}`. All math operations work on vectors of the same size or a vector and a number. All Vectors are truthy.

### array indices

[](#array-indices)

Array indices can be accsessed like `{1,2,3}[0]`

### arrays and functions

[](#arrays-and-functions)

Arrays passed to a method will be received by the php function just like a normal array. Arrays Returned from a php function will be translated to a formula array. The inbuild methods `sizeof()` and `asVector(...args)` can halp working with vectors. Also the `min` and `max` methods are designed to be used with Vectors. They will search recursivly for the min or max value.

Pre defined methods
-------------------

[](#pre-defined-methods)

Those methods are predefined and ready to use in any formula script

php functions
-------------

[](#php-functions)

- `min`
- `max`
- `sqrt`
- `pow`
- `floor`
- `ceil`
- `round`
- `sin`
- `cos`
- `tan`
- `is_nan`
- `abs`

All these are linked to their PHP counterpart and act just like php functions

Additional methods
------------------

[](#additional-methods)

- `asVector(...element)`
- `sizeof()`

Other methods
-------------

[](#other-methods)

- `inRange(value, min, max)` returns true if value is between min and max (including)
- `reduce(valueArray, filterArray)` returns the an array containing all elements that occour both in valueArray and filterArray
- `firstOrNull(array)` returns the first element of the array or null

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance71

Regular maintenance activity

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 85.2% 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 ~10 days

Recently: every ~27 days

Total

105

Last Release

178d ago

Major Versions

1.0.46 → v2.0.22024-06-20

PHP version history (2 changes)1.0.0PHP &gt;=7.4

v2.0.2PHP &gt;=8.1

### Community

Maintainers

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

---

Top Contributors

[![TimoLehnertz](https://avatars.githubusercontent.com/u/57093983?v=4)](https://github.com/TimoLehnertz "TimoLehnertz (184 commits)")[![socialist](https://avatars.githubusercontent.com/u/5237063?v=4)](https://github.com/socialist "socialist (22 commits)")[![cracksalad](https://avatars.githubusercontent.com/u/28577589?v=4)](https://github.com/cracksalad "cracksalad (10 commits)")

---

Tags

formulaphpmathformula

###  Code Quality

TestsPHPUnit

Static AnalysisRector

### Embed Badge

![Health badge](/badges/timo-lehnertz-formula/health.svg)

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

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M276](/packages/brick-math)[markrogoyski/math-php

Math Library for PHP. Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra

2.4k7.1M39](/packages/markrogoyski-math-php)[phpseclib/bcmath_compat

PHP 5.x-8.x polyfill for bcmath extension

16720.7M17](/packages/phpseclib-bcmath-compat)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2751.4M5](/packages/rubix-tensor)[jlawrence/eos

Parse and solve math equations without using 'eval()'.

1071.1M11](/packages/jlawrence-eos)[php-decimal/php-decimal

Correctly-rounded arbitrary precision decimal floating point

781.0M9](/packages/php-decimal-php-decimal)

PHPackages © 2026

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