PHP code example of zippovich2 / expressions-parser
1. Go to this page and download the library: Download zippovich2/expressions-parser 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/ */
use Zippovich2\ExpressionsParser\Parser;
use Zippovich2\ExpressionsParser\OperatorsList;
use Zippovich2\ExpressionsParser\OperatorFactory;
$operators = new OperatorsList();
$operators->add(OperatorFactory::leftAssociative('AND', function ($a, $b){
return $a && $b;
}));
$parser = new Parser($operators);
// false
$parser->eval('1 AND 0');
// true
$parser->eval('1 AND 0');
use Zippovich2\ExpressionsParser\Parser;
use Zippovich2\ExpressionsParser\OperatorsList;
use Zippovich2\ExpressionsParser\OperatorFactory;
$operators = new OperatorsList();
$operators->add(OperatorFactory::leftAssociative('AND', function ($a, $b){
return $a && $b;
}));
$operators->add(OperatorFactory::leftAssociative('OR'));
$defaultCallback = function ($operator, ...$parameters){
switch ($operator){
case 'OR':
return $parameters[0] || $parameters[1];
}
throw new \LogicException('This code should not be reached.');
};
$parser = new Parser($operators);
// true
$parser->eval('1 AND 0 OR 1', $defaultCallback);
// false
$parser->eval('1 AND 0 OR 0', $defaultCallback);
/**
* @throws \LogicException because no callback was provided for "OR" operator.
*/
$parser->eval('1 AND 0 OR 0');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.