PHPackages                             okvpn/expression-language - 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. okvpn/expression-language

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

okvpn/expression-language
=========================

Fast powerful PHP expression language based on twig syntax

1.0.3(1y ago)59.3k↑21.4%1MITPHPPHP &gt;=8.1

Since May 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/okvpn/expression-language)[ Packagist](https://packagist.org/packages/okvpn/expression-language)[ Docs](https://github.com/okvpn/expression-language)[ RSS](/packages/okvpn-expression-language/feed)WikiDiscussions main Synced 2d ago

READMEChangelogDependencies (2)Versions (5)Used By (1)

PHP Expression Language based on TWIG
=====================================

[](#php-expression-language-based-on-twig)

This library provides interfaces for safe evaluate expressions, compile them to native PHP code for performance.

[![Tests](https://github.com/okvpn/expression-language/actions/workflows/tests.yml/badge.svg)](https://github.com/okvpn/expression-language/actions/workflows/tests.yml)[![Latest Stable Version](https://camo.githubusercontent.com/7c9daf93bccee07e7d021114546590c18cc83573b77a3df38596bd3425386dd1/68747470733a2f2f706f7365722e707567782e6f72672f6f6b76706e2f65787072657373696f6e2d6c616e67756167652f76)](https://packagist.org/packages/okvpn/expression-language)[![PHP Version Require](https://camo.githubusercontent.com/2325c776f4d89e6a7ae96a17e0aa3b6b3e0eaba6e4d5a5f83c00414ef22cee3f/68747470733a2f2f706f7365722e707567782e6f72672f6f6b76706e2f65787072657373696f6e2d6c616e67756167652f726571756972652f706870)](https://packagist.org/packages/okvpn/expression-language)

Purpose
-------

[](#purpose)

It is an alternative to the Symfony Expression Language which allows more operations such as `for` `if` etc. You can to implement any logic and execute it safely and quickly. We removed `PRINT_TOKEN` `TEXT_TOKEN` from TWIG AST-tree to prevent output, added `return` statement and made a more improvement.

Features
--------

[](#features)

- Allow to execution scripts and evaluate expressions.
- Debugging with PhpStorm twig debugger feature.
- Support all twig features, like [sandbox](https://twig.symfony.com/doc/3.x/api.html#sandbox-extension), custom extension, tokens, functions.
- Minimum performance overhead - faster that twig.
- Syntax highlighting is already built into the most popular IDEs.

Example Usage
-------------

[](#example-usage)

Base evaluate Example

```
$lang = new TwigLanguage(options: ['cache' => __DIR__ . '/var/cache/twig']);
$context = ['var1' => 10, 'users' => [1, 2, 5], 'user' => 1];

$lang->evaluate('user in users ? users|length + 1 : var1')
```

### Execute Script

[](#execute-script)

```
{% set msg = 'New sms' %}
{% set newUsers = '' %}
{% set usersList = [] %}
{% set lastUserId = redis_get('sms-user') %} # redis_get - custom function

{% for user in users %}
    {% if user > lastUserId %}
        {% set newUsers = newUsers ~ user ~ ' ' %}
        {% do redis_set('sms-user', user, 0) %}
        {% set usersList[_key] = user %} # allow set to array
    {% endif %}
{% endfor %}

{% if newUsers is not empty %}
    {% do telegram_send('chart111', msg ~ "\n" ~ newUsers|trim) %} # telegram_send - custom function
{% endif %}

{% return usersList %} # new return token
```

```
$lang = new TwigLanguage();
$script = execute('test22.twig', $context));
```

`evaluate` method - is faster than `execute`, because we skip additional exception handling and error logic. Also `evaluate` - accept code without `{%` token.

Benchmark Test
--------------

[](#benchmark-test)

We use next the test to compare overhead between Symfony Expression Language and this library

```
$lang = new TwigLanguage(options: [
    'cache' => __DIR__ . '/var/cache/twig',
    'auto_reload' => true,
]);

$expr = 'user == 1 ? var1 + var2 : var1 + 1';
$lang->evaluate($expr, $context);

// Symfony EL
$sf = new ExpressionLanguage();
$expression = $sf->parse($expr, array_keys($context));
$sf->evaluate($expression, $context);

// Native PHP

$fn = static function ($context) {
    return $context['user'] === 1 ? $context['var1'] + $context['var2'] : $context['var1'] + 1;
};

$t1 = microtime(true);
for ($i = 0; $i < 200000; $i++) {
    // comment line where needed
    $result = $fn($context);
    $result = $sf->evaluate($expression, $context);
    $result = $lang->evaluate($expr, $context);
}

echo (microtime(true) - $t1)/200000 * 1000000 . "\n";
```

### Extends and sandbox mode.

[](#extends-and-sandbox-mode)

See [Twig documentation](https://twig.symfony.com/doc/3.x/api.html)

#### Result

[](#result)

LangOp. TimeOverhead costNative PHP0.058µs0%Symfony EL0.461µs+800%Twig lang0.187µs+300%License
=======

[](#license)

MIT License.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

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

Total

4

Last Release

658d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/72ab45c38dd8963c58e34948277405642f3e498f5ffc7508cea6e51a5dcdd24f?d=identicon)[vtsykun](/maintainers/vtsykun)

---

Top Contributors

[![vtsykun](https://avatars.githubusercontent.com/u/21358010?v=4)](https://github.com/vtsykun "vtsykun (19 commits)")

---

Tags

expression-languagesafe-evalevaluate-expressions

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/okvpn-expression-language/health.svg)

```
[![Health](https://phpackages.com/badges/okvpn-expression-language/health.svg)](https://phpackages.com/packages/okvpn-expression-language)
```

PHPackages © 2026

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