dsegw3424 2016-01-01 16:40
浏览 98
已采纳

在PHP IF语句中的多个条件中使用字符串

$mystring is declared as follows:

$mystring = "&& substr($losuj, 0, 1) == 'D' || substr($losuj, 0, 1) == 'K'";

I'd like to use $mystring (it's created by a loop earlier in my code) in the following statement:

while($row = mysql_fetch_array($wynik)) {
    $losuj = $row['wyraz'];
    if (!in_array($losuj, $losowe) 
    $mystring ){.......do something}
}

Is it even doable in PHP? I've tried echoing it normally, with {'$mystring'}, but I guess you can't interrupt loop statement just like that?

  • 写回答

1条回答 默认 最新

  • doudi7782 2016-01-01 16:53
    关注

    It's doable using eval.
    See below a sample using a simpler condition

    $losowe = array('Kool','Awesome');
    
    $losuj = 'Kool';
    
    $a = 'in_array($losuj,$losowe)';
    
    $mystring = ' && substr($losuj, 0, 1) == "K";';
    
    $condition = eval('return ' . $a . $mystring);
    
    if ($condition == true){
        print 'done' . PHP_EOL;
    }
    

    Will output

    done
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?