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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?