我将条件运算符存储在数据库中,因为我们有很多不同的条件,需要将其动态插入到代码中。因此,我要从数据库中提取条件运算符,然后将其存储在变量中。但是然后我实际上需要在if语句中使用运算符。如何将其插入if语句并将其用作常规条件运算符?
这是一些代码示例:
$cond_operator = "<"; // (but this opperator actually pulled from database)
if ( $actual_value $cond_operator $score_value ) {
return $result;
}
可以执行显示的唯一方法是eval()。但是,eval()
由于它引入了许多潜在的安全漏洞,因此人们普遍不赞成使用它。
我将对此进行硬编码,以避免eval()
并确保我可以控制要支持的特定运算符:
$cond_operator = "<"; // (but this operator actually pulled from database)
switch ($cond_operator) {
case "<":
if ( $actual_value < $score_value ) {
return $result;
}
break;
case ">":
if ( $actual_value > $score_value ) {
return $result;
}
break;
case "==":
if ( $actual_value == $score_value ) {
return $result;
}
break;
default:
trigger_error("Unsupported condition operator: '$cond_operator'");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句