PHP code example of leongrdic / smplang

1. Go to this page and download the library: Download leongrdic/smplang 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/ */

    

leongrdic / smplang example snippets


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

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

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

$smpl = new \Le\SMPLang\SMPLang();
$result = $smpl->evaluate('(1 + 2 * 3) / 7');
// $result will be 1

$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"

$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"

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

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

// 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);