PHPackages                             williams/xpression - 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. williams/xpression

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

williams/xpression
==================

Evaluate user supplied mathmematical expressions.

v0.1.0(1y ago)07MITPHP

Since Oct 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/bwilliams-uk/Xpression)[ Packagist](https://packagist.org/packages/williams/xpression)[ RSS](/packages/williams-xpression/feed)WikiDiscussions main Synced 1mo ago

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

Xpression
=========

[](#xpression)

Xpression is a versatile PHP library designed to evaluate string-formatted mathematical expressions, enabling applications to process user-defined formulas similar to those used in spreadsheet applications.

Key features:

- **BODMAS rule support**, with full handling of nested brackets for complex expressions.
- **Variable substitution**, allowing dynamic input and flexible formula construction.
- **Comparison operators** for evaluating expressions with logical conditions.
- **A comprehensive mathematical and logical functions library**, easily extendable with custom functions to suit specific application needs.

Xpression empowers developers to integrate advanced formula evaluation seamlessly, making it ideal for finance, engineering, or other domains requiring dynamic calculations.

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

[](#installation)

Using Composer:

```
composer require williams/xpression
```

Usage
-----

[](#usage)

### Getting Started

[](#getting-started)

A simple example showing how Xpression can resolve an expression with just a few lines of code:

```
require('vendor/autoload.php');
use Williams\Xpression\Xpression;

$xp = Xpression::new();
echo $xp->evaluate('2*3+4'); // 10
```

### Operators

[](#operators)

Xpression offers support for common mathematic and comparison operators.

**Mathematical Operators**

```
$maths = [
    "4^2", // 16 - Powers
    "9/3", // 3  - Division
    "7*5", // 35 - Multiplication
    "5+2", // 7  - Addition
    "4-3", // 1  - Subtracton
];
```

**Comparison Operators**

Comparison operators return either `1` (True) or `0` (False).

```
$comparisons = [
    "2=2",  // 1  - Equal to
    "42",  // 1  - Greater Than
    "7=9", // 0  - Greater Than or Equal to
    "26", // 1  - Not Equal To
];
```

### Variables

[](#variables)

Xpression provides two ways for using variables in expressions. The `with` method allows setting of variable values with an associative array. Alternatively, an on-the-fly lookup can be achieved by using a [Variable Resolver](docs/variable-resolvers.md).

Using the `with` method to set variables:

```
// Example using variables:
$xp->with([
 'a' => 7,
 'b' => 5
]);

echo $xp->evaluate('$a-$b'); // 2
```

The `affix` method can be used to change how variables are denoted. Supply one parameter to define a prefix:

```
$xp->affix('%');
echo $xp->evaluate('%a-%b');
```

Alternatively, supply two parameters for encapsulation:

```
$xp->affix('{','}');
echo $xp->evaluate('{a}-{b}');
```

### Functions

[](#functions)

A number of [built-in functions](docs/supported-functions.md) are available. Below is an example using the `MIN` function:

```
echo $xp->evaluate('MIN($a,$b)'); // 5
```

If further functionality is required, you can define your own [custom functions](docs/custom-functions.md).

Further Topics
--------------

[](#further-topics)

- [Operator Precedence](docs/operator-precedence.md)
- [Exception Handling](docs/exception-handling.md)
- [Testing](docs/testing.md)

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

588d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c99edde3f9f8c1bf87a077618cd96dad78a63e69e579965ad9dd17b4e078264?d=identicon)[bwilliams-uk](/maintainers/bwilliams-uk)

---

Top Contributors

[![bwilliams-uk](https://avatars.githubusercontent.com/u/38534904?v=4)](https://github.com/bwilliams-uk "bwilliams-uk (1 commits)")

### Embed Badge

![Health badge](/badges/williams-xpression/health.svg)

```
[![Health](https://phpackages.com/badges/williams-xpression/health.svg)](https://phpackages.com/packages/williams-xpression)
```

###  Alternatives

[swissup/module-slick-carousel

the last carousel you'll ever need for magento2

1020.8k1](/packages/swissup-module-slick-carousel)

PHPackages © 2026

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