转换在 If 条件下从数据库检索的字符串

Convert string which retrieve from database in a If condition

我在数据库的一个列中存储了一些 rules/logic。有了那个重试的规则,我想把它作为一个条件。由于检索到的规则作为字符串值返回,因此我的条件始终为 TRUE。

假设

$www = $rule->logic; // $rule ->logic = (20 >  3) and (20 <  5)
    if($www ){                          
        $this->logger->debug('commision granted');
            }else{
        $this->logger->debug('commision noooo');
            }

但是如果我简单地使用if ((20 > 3) and (20 < 5))那么我可以得到正确的答案。 我不知道如何转换 $www ?

你可以使用PHP eval()来执行表达式:

$www = $rule->logic; // $rule ->logic = (20 >  3) and (20 <  5)
if(eval("return $www;")){                          
    echo 'commision granted';
        }else{
    echo 'commision noooo';
        }