PHPackages                             freezewarp/php-shunting-yard - 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. freezewarp/php-shunting-yard

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

freezewarp/php-shunting-yard
============================

Flexible shunting yard parser, based on https://github.com/andig/php-shunting-yard

1.2.1(6y ago)3160↓100%MITPHPPHP &gt;=7

Since Nov 26Pushed 5y ago2 watchersCompare

[ Source](https://github.com/FreezeWarp/php-shunting-yard)[ Packagist](https://packagist.org/packages/freezewarp/php-shunting-yard)[ Docs](https://github.com/FreezeWarp/php-shunting-yard)[ RSS](/packages/freezewarp-php-shunting-yard/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (1)Versions (11)Used By (0)

PHP Shunting Yard Implementation
--------------------------------

[](#php-shunting-yard-implementation)

### Fork Changes

[](#fork-changes)

The general goal of this fork is to take the existing framework for safe formula evaluation and add the needed functionality for more general inputs (whereas the original is fairly heavily focused on numbers alone).

To this end, the following changes are made:

- Constants can be any PHP value, not just numeric values. (Internally, they are represented as `T_NATIVE`, and subsume `T_NULL`.)
- Support for string literals has been added.

    - The `||` operator was added for concatenation. (This operator is given precedence above equality but below addition and subtraction. Thus, `2 + 3 || 3 + 4 = "57"`.)
    - The `+` operator acts as concatenation if either side is non-numeric. (Thus, `"2" + "3" = 5`, but `"2 " + "3" = "2 3"`)
    - String literals are supported -- either as `"string"` or `'string'`. (Escaping is not supported.)
- Support for array literals has been added.

    - The `||` operator will perform array merging if either operand is an array.
    - The `in` operator can be used to check the existence of an array member. A `not in` operator still needs to be added.
    - Both lists and associative arrays are implemented. Lists use the syntax `[1, 2, 3]`, while associative arrays use the syntax `[1 -> 2, 3 -> 4, 5 -> 6]`.
    - Array keys and values support all operations otherwise supported by shunting expressions -- for instance, `[1 + 2 -> 3 + 4, 5 + 6 -> 7 + 8]` is equivalent to `[3 -> 7, 11 -> 15]`
- Variable names have been expanded:

    - Now, any string matching `/\p{L}\p{N}\.]+/` (that is, containing only unicode letters, numbers, and the symbol `.`) will be treated as a variable name. This means that `littérature + 手紙` is a valid formula that is adding two variables.
    - A special syntax, `${xyz}` is allotted for more complex variable references -- this supports any character in the variable name other than `}`.
    - Unregistered variables evaluate to 0 instead of causing an exception. A strict mode flag on Context can re-enable the old behaviour.
- End-of-line comments are supported using #
- A handful of more opinionated changes were made to make the overall syntax more user-friendly:

    - No default constants are set. At construction time, an array can be passed to set all constants at once.
    - `and`, `or`, and `not` are now synonyms for `&`, `|`, and `!` respectively.
    - `if`, `coalesce`, `min`, and `max` functions are registered by default.
    - The xor operator was removed to avoid possible confusion (it was `>constants, $name);
    }
}

```

While the default class only performs normal array lookups, there are many opportunities for more advanced array lookups. Laravel's array\_get provides basic dot lookups, or  could be used for full JSONPath lookups.

### Example

[](#example)

Simple equation parsing

```
use RR\Shunt\Parser;

$equation = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3';
$result = Parser::parse($equation);
echo $result; //3.0001220703125
```

Equation with constants and functions

```
use RR\Shunt\Parser;
use RR\Shunt\Context;

$ctx = new Context();
$ctx->def('abs'); // wrapper for PHP "abs" function
$ctx->def('foo', 5); // constant "foo" with value "5"
$ctx->def('bar', function($a, $b) { return $a * $b; }); // define function

$equation = '3 + bar(4, 2) / (abs(-1) - foo) ^ 2 ^ 3';
$result = Parser::parse($equation, $ctx);
echo $result; //3.0001220703125
```

Test a condition

```
use RR\Shunt\Parser;
use RR\Shunt\Context;

$ctx = new Context();
$ctx->def('foo', 5); // constant "foo" with value "5"

$equation = '(foo > 3) & (foo < 6)';
$result = Parser::parse($equation, $ctx);
echo $result; //true
```

Re-run parsed expression on multiple inputs

```
use RR\Shunt\Parser;
use RR\Shunt\Context;

$counter = 1;
$ctx = new Context();
$ctx->def('data', function() { global $counter; return $counter++; }); // define function
$ctx->def('bar', function($a) { return 2*$a; }); // define function

$equation = 'bar(data())';
$parser = new Parser(new Scanner($equation));

$result = $parser->reduce($this->ctx); // first result
echo $result; // 2
$result = $parser->reduce($this->ctx); // second result
echo $result; // 4
```

### Installation

[](#installation)

Define the following requirement in your composer.json file:

```
{
    "require": {
        "freezewarp/php-shunting-yard": "dev-master"
    }
}
```

### Authors

[](#authors)

This is a fork of , which in turn used code from:

-
-
-
-
-

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~128 days

Recently: every ~7 days

Total

9

Last Release

2482d ago

PHP version history (2 changes)1.0PHP &gt;=5.3.3

1.1PHP &gt;=7

### Community

Maintainers

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

---

Top Contributors

[![andig](https://avatars.githubusercontent.com/u/184815?v=4)](https://github.com/andig "andig (25 commits)")[![FreezeWarp](https://avatars.githubusercontent.com/u/933133?v=4)](https://github.com/FreezeWarp "FreezeWarp (17 commits)")[![sergej-kurakin](https://avatars.githubusercontent.com/u/196096?v=4)](https://github.com/sergej-kurakin "sergej-kurakin (15 commits)")[![pmishev](https://avatars.githubusercontent.com/u/5188763?v=4)](https://github.com/pmishev "pmishev (14 commits)")[![falahati](https://avatars.githubusercontent.com/u/2479372?v=4)](https://github.com/falahati "falahati (10 commits)")[![dmromanov](https://avatars.githubusercontent.com/u/299059?v=4)](https://github.com/dmromanov "dmromanov (1 commits)")

---

Tags

mathshunting yardequation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/freezewarp-php-shunting-yard/health.svg)

```
[![Health](https://phpackages.com/badges/freezewarp-php-shunting-yard/health.svg)](https://phpackages.com/packages/freezewarp-php-shunting-yard)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/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.1M40](/packages/markrogoyski-math-php)[phpseclib/bcmath_compat

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

16720.7M17](/packages/phpseclib-bcmath-compat)[andig/php-shunting-yard

Refactored repack of https://github.com/droptable/php-shunting-yard

26219.4k](/packages/andig-php-shunting-yard)[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)

PHPackages © 2026

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