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.