PHP code example of maximaster / evalue

1. Go to this page and download the library: Download maximaster/evalue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maximaster / evalue example snippets


use Maximaster\Evalue\Evalue;

// Create an Evalue instance with PHP code
$evalue = new Evalue('return $number * 2;');

// Run the code with context
$result = $evalue->run(['number' => 21]); // Returns 42

$evalue = new Evalue(
    'return $greeting . " " . $name;',
    ['greeting' => 'Hello']
);

// Run with additional context
$result = $evalue->run(['name' => 'John']); // Returns "Hello John"

// Multi-line code
$code = <<<'PHP'
    $sum = 0;
    for($i = 1; $i <= $max; $i++) {
        $sum += $i;
    }
    return $sum;
PHP;

$evalue = new Evalue($code);
$result = $evalue->run(['max' => 5]); // Returns 15

$evalue = new Evalue(
    'return $name;',
    ['name' => 'John']
);

// Constructor context can be overridden in run()
$result = $evalue->run(['name' => 'Jane']); // Returns "Jane"

$evalue->run([
    '123name' => 'invalid',    // Invalid: starts with number
    'my-var' => 'invalid',     // Invalid: contains hyphen
    'my var' => 'invalid'      // Invalid: contains space
]);