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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 arduino控制ps2手柄一直报错
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题