将运算符符号保存在 PHP 变量中

Save operator symbol in PHP variable

我想做的就是从数据库中获取保存的运算符并在 if else 条件下使用它。这是我的代码。它总是 return 正确。无法弄清楚我做错了什么

$sql_c = "select cc.operator, cc.dpt_id, dp.dpt_value as value from criteria_condition cc
                join decision_point_type dp on cc.dpt_id = dp.dpt_id where criteria_id = $nodecriteria";
        $res_c = mysql_query($sql_c);
        while($row = mysql_fetch_array($res_c)){
             $operand = $row['operator'];
             $dpt_value = $row['value'];
        }
        echo "'$userinput'".$operand ."'$dpt_value'".'<br>';
        if("'$userinput'".$operand."'$dpt_value'"){
            echo 'true';
        }else{
            echo 'false';
        }

Echo 显示 "hello"==="hello" 但在 if 条件下它不能正常工作

你不能像那样使用字符串作为运算符。

看看这个:

您可以使用 eval(),但最安全的方法如下:

$left = (int)$a;
$right = (int)$b;
$result = 0;
switch($char){

  case "*":
    $result = $left * $right;
    break;

 case "+";
   $result = $left + $right;
   break;
// etc

}

资源:

PHP use string as operator