PHPackages                             zippovich2/expressions-parser - 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. zippovich2/expressions-parser

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

zippovich2/expressions-parser
=============================

Used shunting-yard algorithm to parse any expressions and RPN(Reverse Polish Notations) to execute it.

v1.0.1(5y ago)0484MITPHPPHP &gt;=7.3

Since May 6Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Zippovich2/expressions-parser)[ Packagist](https://packagist.org/packages/zippovich2/expressions-parser)[ RSS](/packages/zippovich2-expressions-parser/feed)WikiDiscussions main Synced today

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

Expressions Parser
==================

[](#expressions-parser)

Used shunting-yard algorithm to convert any expressions to RPN(Reverse Polish Notations) and process it.

[![Tests](https://github.com/zippovich2/expressions-parser/actions/workflows/ci-tests.yaml/badge.svg?branch=main)](https://github.com/Zippovich2/expressions-parser/actions/workflows/ci-tests.yaml)[![Packagist](https://camo.githubusercontent.com/89730a9a9f6e76101b48d4c768844d74587bbe9a1600dd14d19f1d0212e200c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a6970706f76696368322f65787072657373696f6e732d7061727365722e737667)](https://packagist.org/packages/zippovich2/expressions-parser)

- [Installation](#installation)
- [Predefined parsers](#predefined-parsers)
    - [Arithmetical](#arithmetical-expressions-parser)
    - [Logical](#logical-expressions-parser)
- [Extending](#extending)
- [Custom parser](#custom-parser)
- [Operators](#operators)
    - [Types](#types)
    - [Reserved names](#reserved-names)
- [Callbacks](#callbacks)
- [References](#references)

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

[](#installation)

`composer require zippovich2/expressions-parser`

Predefined parsers
------------------

[](#predefined-parsers)

### Arithmetical expressions parser

[](#arithmetical-expressions-parser)

```
use Zippovich2\ExpressionsParser\Parser\ArithmeticalParser;

$parser = new ArithmeticalParser();

// 45
$parser->eval('2 + (3^3) + 8 * (3 - 1)');

// 3.0001220703125
$parser->eval('3 + 4 * 2 / (1 - 5) ^ 2 ^ 3');

// 58
$parser->eval('58+(max(1, 2, 3)/3)');
```

### Logical expressions parser

[](#logical-expressions-parser)

```
use Zippovich2\ExpressionsParser\Parser\LogicalParser;

$parser = new LogicalParser();

// true
$parser->eval('true || false');

// false
$parser->eval('true && false');

// true
$parser->eval('true xor false');

// false
$parser->eval('true xor true');
```

Extending
---------

[](#extending)

```
use Zippovich2\ExpressionsParser\Parser\LogicalParser;
use Zippovich2\ExpressionsParser\OperatorFactory;

$parser = new LogicalParser();
$parser->addOperator(OperatorFactory::rightAssociative('**', function($a, $b){
    return $a ** $b;
}, 5));
$parser->addOperator(OperatorFactory::func('if', function($condition, $if, $else){
    return $condition ? $if : $else;
}));
$parser->addOperator(OperatorFactory::constant('e', M_E));

// false
$res = $parser->eval('if(2 < 1, true, false)');

// true
$res = $parser->eval('if(2 > 1, true, false)');

// 16
$res = $parser->eval('2**4');

// 3.718281828459
$res = $parser->eval('e+1');
```

Custom parser
-------------

[](#custom-parser)

```
use Zippovich2\ExpressionsParser\Parser;
use Zippovich2\ExpressionsParser\OperatorsList;
use Zippovich2\ExpressionsParser\OperatorFactory;

$operators = new OperatorsList();

$operators->add(OperatorFactory::leftAssociative('AND', function ($a, $b){
    return $a && $b;
}));

$parser = new Parser($operators);

// false
$parser->eval('1 AND 0');

// true
$parser->eval('1 AND 0');
```

Operators
---------

[](#operators)

### Types

[](#types)

1. `Operator::TYPE_LEFT_ASSOCIATIVE` - left associative.
2. `Operator::TYPE_RIGHT_ASSOCIATIVE` - right associative.
3. `Operator::TYPE_FUNCTION` - function.
4. `Operator::TYPE_LEFT_ASSOCIATIVE` - constant.

### Reserved names

[](#reserved-names)

An operator cannot be created using the characters `(` and `)` because it is used for groups and `,` because it is used for separating function parameters.

Callbacks
---------

[](#callbacks)

You can provide callback for each operator or create global callback to handle all operators.

```
use Zippovich2\ExpressionsParser\Parser;
use Zippovich2\ExpressionsParser\OperatorsList;
use Zippovich2\ExpressionsParser\OperatorFactory;

$operators = new OperatorsList();

$operators->add(OperatorFactory::leftAssociative('AND', function ($a, $b){
    return $a && $b;
}));

$operators->add(OperatorFactory::leftAssociative('OR'));

$defaultCallback = function ($operator, ...$parameters){
    switch ($operator){
        case 'OR':
            return $parameters[0] || $parameters[1];
    }

    throw new \LogicException('This code should not be reached.');
};

$parser = new Parser($operators);

// true
$parser->eval('1 AND 0 OR 1', $defaultCallback);

// false
$parser->eval('1 AND 0 OR 0', $defaultCallback);

/**
 * @throws \LogicException because no callback was provided for "OR" operator.
 */
$parser->eval('1 AND 0 OR 0');
```

References
----------

[](#references)

- [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm)
- [Operator](https://en.wikipedia.org/wiki/Operator_(computer_programming))
- [Operator associativity](https://en.wikipedia.org/wiki/Operator_associativity)
- [Order of operations or operator precedence](https://en.wikipedia.org/wiki/Order_of_operations)
- [Reverse Polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation)

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 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

Every ~7 days

Total

2

Last Release

1876d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/26221873?v=4)[Skoropadskyi Roman](/maintainers/Zippovich2)[@Zippovich2](https://github.com/Zippovich2)

---

Top Contributors

[![Zippovich2](https://avatars.githubusercontent.com/u/26221873?v=4)](https://github.com/Zippovich2 "Zippovich2 (6 commits)")

---

Tags

mathshunting yardequationRPNreverse polish notationsarithmetical expressions parserlogical expressions parserexpressions parser

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/zippovich2-expressions-parser/health.svg)

```
[![Health](https://phpackages.com/badges/zippovich2-expressions-parser/health.svg)](https://phpackages.com/packages/zippovich2-expressions-parser)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.2k548.2M417](/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.6M52](/packages/markrogoyski-math-php)[phpseclib/bcmath_compat

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

16821.5M25](/packages/phpseclib-bcmath-compat)[andig/php-shunting-yard

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

26226.9k](/packages/andig-php-shunting-yard)[rubix/tensor

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

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

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

1071.2M11](/packages/jlawrence-eos)

PHPackages © 2026

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