PHP code example of hoanglongtrinh / logic-expression

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

    

hoanglongtrinh / logic-expression example snippets


use LogicExpression\Logic;

$logic = new Logic();

$result = $logic->IF(
    fn() => 1 == 1,
    fn() => 'YES',
    fn() => 'NO'
); // 👉 'YES'


use LogicExpression\LogicParserService;

$parser = LogicParserService::getInstance();

$result = $parser->IFS(
    1 == 2, 100,
    $parser->IF(3 == 3, 200, 300),
    1
); // 👉 200


use function parseAllLogicExpressions;
$messages = [
    'ifs_excel' => 'Lỗi logic phân loại khách hàng',
    'if_excel'  => 'Sai biểu thức điều kiện IF tại bước xác minh',
    'and_excel' => 'AND không hợp lệ khi kết hợp điều kiện doanh số',
    'or_excel'  => 'OR bị lỗi trong phân tích KPI',
    'not_excel' => 'NOT dùng sai trong lọc báo cáo'
];

$expr = "IFS(1=2, 4, IF(3=1, 4, 0), IF(IF(3=3,7,0)=0,3,5)=5, 2, 1)";
echo parseAllLogicExpressions($expr, $messages); // 👉 "2"


$input = "IFS(1=2, 4, IF(3=1, 4, 0), IF(IF(3=3,7,0)=0,3,5)=5, 2, 1) + AND(1=1, 2=2)";
$output = parseAllLogicExpressions($input, $messages); // 👉 "2 + 1"
eval("echo $output;"); // 👉 "3"