动态条件字符串

时间:2015-09-10 07:51:03

标签: php eval

我将以下代码部分放在函数中:

$dynamic_comparison = '';  
if(($from == '')&&($to == '')){
    $dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
    $dynamic_comparison = '($row >= $from) && ($row <= $to)';
}else if(($from != '')&&($to == '')){
    $dynamic_comparison = '($row >= $from)';
}else if(($from == '')&&($to != '')){
    $dynamic_comparison = '($row <= $to)';
}

$form$to$row是该函数的参数。

我想将$dynamic_comparison评估为类似的内容:

if($dynamic_comparison){
    //A bunch of code here...
}

我试过了:

if(eval($dynamic_comparison)){
    //A bunch of code here...
}

它会抛出错误。如何做到这一点?

0 个答案:

没有答案