PHPackages                             leongrdic/smplang - 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. leongrdic/smplang

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

leongrdic/smplang
=================

A simple language written in PHP that evaluates expressions without eval

1.0.2(3y ago)16641.5k↓17.3%4[1 issues](https://github.com/leongrdic/php-smplang/issues)[2 PRs](https://github.com/leongrdic/php-smplang/pulls)4MITPHPPHP &gt;=8.0

Since Jun 13Pushed 2y ago2 watchersCompare

[ Source](https://github.com/leongrdic/php-smplang)[ Packagist](https://packagist.org/packages/leongrdic/smplang)[ Docs](https://github.com/leongrdic/php-smplang)[ RSS](/packages/leongrdic-smplang/feed)WikiDiscussions master Synced 1mo ago

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

SMPLang
=======

[](#smplang)

[![release](https://camo.githubusercontent.com/277aa6e1c286466d950c0182d2853551f518be48fc41a2cd4a9c2c70a508bffd/687474703a2f2f706f7365722e707567782e6f72672f6c656f6e67726469632f736d706c616e672f76)](https://packagist.org/packages/leongrdic/smplang)[![php-version](https://camo.githubusercontent.com/2042cec77b2620c624a779840cd6fc7e6c49d0d59f7ee8848493f9afc0fbab50/687474703a2f2f706f7365722e707567782e6f72672f6c656f6e67726469632f736d706c616e672f726571756972652f706870)](https://packagist.org/packages/leongrdic/smplang)[![license](https://camo.githubusercontent.com/1f58e218ae7982ae1d48b4a65d3a2586dae7e80ac806cb362f65317d521bf47e/687474703a2f2f706f7365722e707567782e6f72672f6c656f6e67726469632f736d706c616e672f6c6963656e7365)](https://packagist.org/packages/leongrdic/smplang)[![run-tests](https://github.com/leongrdic/php-smplang/actions/workflows/run-tests.yml/badge.svg)](https://github.com/leongrdic/php-smplang/actions/workflows/run-tests.yml)

[![try](https://camo.githubusercontent.com/924d4af177e0a5033014814d7d5270d7fc1330af98abef13529604f3432b89e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54727925323069742532306f75742d6f6e25323050485053616e64626f782d253233374532394345)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%27foo%27%20%3D%3E%205%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27%281%20%2B%20foo%20%2A%204%29%20%2F%207%27%29%3B%0A%0Aprint_r%28%24result%29%3B)

SMPLang is a simple expression language written in PHP. It can be considered similar to PHP's native `eval()` function but SMPLang has its own syntax and the expressions are evaluated in sort-of a sandbox with access to only vars and functions/closures that you pass into it.

The language is partly inspired by Symfony Expression Language but there are some major differences like array unpacking, named arguments and easier function definition, thus SMPLang may not be a replacement for some use cases.

Install:

```
composer require leongrdic/smplang

```

To use SMPLang, create a new instance of the `\Le\SMPLang\SMPLang` class and pass in an associative array of vars that you want to use in your expressions.

```
$smpl = new \Le\SMPLang\SMPLang([
    'variableName' => 'value',
]);
```

You can then call the `evaluate()` method on the instance, passing in an expression string. This will return the result of the expression.

```
$result = $smpl->evaluate($expression);
```

Optionally, you can pass in an associative array to the second parameter to provide additional local vars to use in the expression:

```
$result = $smpl->evaluate($expression, [
    'localVariable' => 123,
]);
```

Vars passed this way will override vars passed in the constructor and will only be available in this specific expression, in case multiple expressions are evaluated from the same object.

In case of an exception, `\Le\SMPLang\Exception` will be thrown with a short description of the error.

Expression syntax
-----------------

[](#expression-syntax)

Vars are accessed by only their name. If a var is not defined in neither constructor or `evaluate()`, an exception will be thrown.

### Supported literals

[](#supported-literals)

- `null`
- booleans (`true` and `false`)
- strings (`"string"` or `'string'` or ``string``)
- numbers (`1`, `1.2`, `-1`, `-1.2`)
- arrays (`[23, 'string']` or `["key": 23, 'key2': 'string']`)
- objects (`{foo: "bar", baz: 23}`)

### Arrays

[](#arrays)

Array definition: `[element1, element2]`

Associative array definition: `["key": element, string_variable: element2]`

You can define associative arrays with dynamic keys by using string vars in place of keys.

Array unpacking is supported: `[element1, ...array, ...array2]`

Access array elements using either of the following syntaxes: `array.key.0` or `array['key'][0]` (which allows for dynamic array access).

### Objects

[](#objects)

Object definition: `{foo: "bar", baz: 23}` (supports array unpacking)

Object property access: `object.property`

Object method call: `object.method(parameters)`

### Function / closure call

[](#function--closure-call)

Call a function or closure var: `closure_var(param1, param2)`.

Named arguments: `foo(search: value, count: 1)`.

Function / closure calls support array unpacking: `bar(param1, ...array, ...array2)`

### Arithmetic operators

[](#arithmetic-operators)

- `+`: addition
- `-`: subtraction
- `*`: multiplication
- `/`: division
- `%`: modulo (`a*b%c*d == (a*b)%(c*d)`)
- `**`: exponentiation (`a*b**c*d == (a*b)**(c*d)`)

### Comparison operators

[](#comparison-operators)

- `===`: strict equality
- `!==`: strict inequality
- `==`: equality
- `!=`: inequality
- `>`: greater than
- `=`: greater than or equal to
- `evaluate('(1 + 2 * 3) / 7');
// $result will be 1
```

[![try](https://camo.githubusercontent.com/924d4af177e0a5033014814d7d5270d7fc1330af98abef13529604f3432b89e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54727925323069742532306f75742d6f6e25323050485053616e64626f782d253233374532394345)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%29%3B%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27%281%20%2B%202%20%2A%203%29%20%2F%207%27%29%3B%0Aprint_r%28%24result%29%3B)

```
$smpl = new \Le\SMPLang\SMPLang([
    'foo' => 'bar',
    'arr' => [1, 2, 3],
    'hash' => ['a' => 'b'],
]);

$result = $smpl->evaluate('foo ~ " " ~ arr[1] ~ " " ~ hash.a');
// $result will be "bar 2 b"
```

[![try](https://camo.githubusercontent.com/924d4af177e0a5033014814d7d5270d7fc1330af98abef13529604f3432b89e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54727925323069742532306f75742d6f6e25323050485053616e64626f782d253233374532394345)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%20%20%27foo%27%20%3D%3E%20%27bar%27%2C%0A%20%20%20%20%27arr%27%20%3D%3E%20%5B1%2C%202%2C%203%5D%2C%0A%20%20%20%20%27hash%27%20%3D%3E%20%5B%27a%27%20%3D%3E%20%27b%27%5D%2C%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27foo%20~%20%22%20%22%20~%20arr%5B1%5D%20~%20%22%20%22%20~%20hash.a%27%29%3B%0Aprint_r%28%24result%29%3B)

```
$smpl = new \Le\SMPLang\SMPLang([
    'prepend' => fn(string $a): string => "hello $a",
    'reverse' => strrev(...),
]);

$result = $smpl->evaluate('prepend("simple " ~ reverse("world"))');
// $result will be "hello simple dlrow"
```

[![try](https://camo.githubusercontent.com/924d4af177e0a5033014814d7d5270d7fc1330af98abef13529604f3432b89e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54727925323069742532306f75742d6f6e25323050485053616e64626f782d253233374532394345)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%20%20%27prepend%27%20%3D%3E%20fn%28string%20%24a%29%3A%20string%20%3D%3E%20%22hello%20%24a%22%2C%0A%20%20%20%20%27reverse%27%20%3D%3E%20strrev%28...%29%2C%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27prepend%28%22simple%20%22%20~%20reverse%28%22world%22%29%29%27%29%3B%0Aprint_r%28%24result%29%3B)

```
$smpl = new \Le\SMPLang\SMPLang([
    'foo' => 'bar',
]);

$result = $smpl->evaluate('foo !== "bar" ? "yes" : "no"');
// $result will be "no"
```

[![try](https://camo.githubusercontent.com/924d4af177e0a5033014814d7d5270d7fc1330af98abef13529604f3432b89e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54727925323069742532306f75742d6f6e25323050485053616e64626f782d253233374532394345)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%20%20%27foo%27%20%3D%3E%20%27bar%27%2C%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27foo%20%21%3D%3D%20%22bar%22%20%3F%20%22yes%22%20%3A%20%22no%22%27%29%3B%0Aprint_r%28%24result%29%3B)

```
// SMPL can even be used as an (incredibly slow) JSON parser!

$smpl = new \Le\SMPLang\SMPLang();
$json = '{ "foo": null, "bar": 15.23, "baz": true, "arr": [5, 6], "str": "cool" }';
$result = $smpl->evaluate($json);
print_r($result);
```

[![try](https://camo.githubusercontent.com/924d4af177e0a5033014814d7d5270d7fc1330af98abef13529604f3432b89e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54727925323069742532306f75742d6f6e25323050485053616e64626f782d253233374532394345)](https://play.phpsandbox.io/leongrdic/smplang?input=%3C%3Fphp%0A%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%29%3B%0A%24json%20%3D%20%27%7B%20%22foo%22%3A%20null%2C%20%22bar%22%3A%2015.23%2C%20%22baz%22%3A%20true%2C%20%22arr%22%3A%20%5B5%2C%206%5D%2C%20%22str%22%3A%20%22cool%22%20%7D%27%3B%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%24json%29%3B%0Aprint_r%28%24result%29%3B)

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

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

Total

3

Last Release

1406d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/82e25ee9c2189d89148b77cb15477330861242577535ff77a73773aba317e369?d=identicon)[leongrdic](/maintainers/leongrdic)

---

Top Contributors

[![leongrdic](https://avatars.githubusercontent.com/u/6260068?v=4)](https://github.com/leongrdic "leongrdic (52 commits)")[![isaeken](https://avatars.githubusercontent.com/u/57031552?v=4)](https://github.com/isaeken "isaeken (8 commits)")[![Yohn](https://avatars.githubusercontent.com/u/2002591?v=4)](https://github.com/Yohn "Yohn (1 commits)")

---

Tags

evalevaluationevaluatorexpressionexpression-evaluatorexpression-languagelanguagephpphp8sandbox

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/leongrdic-smplang/health.svg)

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

###  Alternatives

[qieangel2013/esparser

es php library

1583.4k](/packages/qieangel2013-esparser)[yii2mod/yii2-c3-chart

Yii2 wrapper for D3-based reusable chart library

1193.6k](/packages/yii2mod-yii2-c3-chart)

PHPackages © 2026

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