【问题标题】:Save operator symbol in PHP variable在 PHP 变量中保存运算符符号
【发布时间】:2015-02-24 12:28:28
【问题描述】:

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

$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 条件下无法正常工作

【问题讨论】:

标签: php phpmyadmin


【解决方案1】:

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

看看这个:

您可以使用 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

【讨论】:

    猜你喜欢
    • 2013-03-04
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多