douyuben9434 2019-04-06 18:33
浏览 316

PHP Eval替代生成动态if条件

I have the following php code where I'm trying to generate a dynamic if condition using eval:

$categoryId = $_REQUEST['category_id'];
$locationId = $_REQUEST['location_id'];

$recordId = 1;

$criteria = [(!empty($categoryId) ? '(isInCategory($recordId, $categoryId))' : ''),
             (!empty($locationId) ? '(isInLocation($recordId, $locationId))' : '')];

$check = implode(' && ', array_filter($criteria));

// $check will be  
// (isInCategory($recordId, $categoryId)) && (isInLocation($recordId, $locationId))
// if $categoryId and $locationId are not empty

if(eval("return $check;")) 
{ 
    echo 'true'; 
}
else
{
   echo 'false';
}

The problem is the entire code itself is within an eval statement so basically I'm ending up with an eval inside an eval which is throwing an error - can't really troubleshoot the error because all I get is a friendly error page so I'm assuming that is the case i.e. you cant use eval inside another eval???

I'm forced to use a propriety product so this is my limitation. Is there a way I can refactor the eval in the above if statement via some other technique.

I read some stuff about using call_user_func() but I'm new to this. Any help would be appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 matlab求解平差
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办