PHPackages                             manychois/peval - 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. manychois/peval

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

manychois/peval
===============

A PHP package for evaluating expressions with support for variables, functions, and operators.

v0.1.0-beta(11mo ago)14[5 PRs](https://github.com/manychois/peval/pulls)MITPHPPHP ^8.4CI passing

Since Jun 11Pushed 3mo agoCompare

[ Source](https://github.com/manychois/peval)[ Packagist](https://packagist.org/packages/manychois/peval)[ RSS](/packages/manychois-peval/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (9)Used By (0)

Peval - PHP Expression Evaluator
================================

[](#peval---php-expression-evaluator)

[![PHP Version](https://camo.githubusercontent.com/2ef1b311391a967fe24849a49dd4d8e1b8ee9cf1f15625fd7c7b11a7c78da800/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e342d3737376262332e737667)](https://www.php.net/releases/8.4/en.php)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](LICENSE)

Peval is a secure PHP library for parsing and evaluating expressions with support for variables, functions, operators, objects and array structures. Built on top of the excellent [`nikic/php-parser`](https://github.com/nikic/PHP-Parser) library, Peval provides a safe and controlled environment for executing dynamic expressions at runtime without the risks associated with `eval()`. All unsafe PHP functions are strictly prohibited to ensure security.

Features
--------

[](#features)

CategoryDescriptionExamples**🔢 Arithmetic**Arithmetic operations`+`, `-`, `*`, `/`, `%`, `**`, `()`**🔍 Comparison**Value and type comparison operators`==`, `!=`, `===`, `!==`, `=`, ``**🧠 Logical**Boolean logic operations`&&`, `||`, `!`, `and`, `or`, `xor`**📚 Arrays**Array creation and access operations`[1, 2, 3]`, `$arr[0]`, `['a' => 'Apple']`**🔤 String**String manipulation and interpolation`'abc'`, `"Hello {$name}!"`, `.`**📝 Variables**Dynamic variable resolution with context`$variable`**🔑 Object Access**Object properties and constants`$obj->prop`, `Class::CONST`**📞 Function Calls**Function and object method calls`func()`, `$obj->method()`**➡️ Arrow Functions**Arrow function syntax for concise callbacks`array_map(fn($x) => $x * 2, $array)`**🔒 Security**Unsafe functions prohibitedNo `eval()`, `exec()`, file operations, etc.**❓ Others**Ternary and null coalescing`? :`, `??`Installation
------------

[](#installation)

```
composer require manychois/peval
```

Requirements
------------

[](#requirements)

- PHP 8.4 or higher

Security
--------

[](#security)

Peval prioritizes security by maintaining a comprehensive blacklist of unsafe PHP functions that are prohibited from execution. This includes:

- **Code execution functions**: `eval()`, `exec()`, `system()`, etc.
- **File system operations**: `file_get_contents()`, `unlink()`, `chmod()`, etc.
- **Network functions**: `curl_exec()`, `mail()`, etc.
- **Reflection capabilities**: `get_defined_functions()`, `class_exists()`, etc.

This ensures that expressions can only perform safe computations without accessing external resources or executing potentially dangerous operations.

For a complete list of prohibited functions, please refer to the [src/unsafe.php](src/unsafe.php) file in the source code.

Quick Start
-----------

[](#quick-start)

```
