Using a variable as an operator
No, that syntax isn't available. The best you could do would be an eval(), which would not be recommended, especially if the $e came from user input (ie, a form), or a switch statement with each operator as a case
switch($e)
{
case "||":
if($a>$b || $c>$d)
echo 'yes';
break;
}
It's not possible, but you could use a function instead. Of course, you'd have to define them yourself. This would be fairly simple using PHP 5.3's closures:
$or = function($x, $y)
{
return $x || $y;
};
if ($or($a > $b, $c > $d))
{
echo 'yes';
};
Here's a function to add some vague and undefined "additional criteria" for narrowing down a list of products.
/**
* Criteria checker
*
* @param string $value1 - the value to be compared
* @param string $operator - the operator
* @param string $value2 - the value to test against
* @return boolean - criteria met/not met
*/
protected function criteriaMet($value1, $operator, $value2)
{
switch ($operator) {
case '<':
return $value1 < $value2;
case '<=':
return $value1 <= $value2;
case '>':
return $value1 > $value2;
case '>=':
return $value1 >= $value2;
case '==':
return $value1 == $value2;
case '!=':
return $value1 != $value2;
default:
return false;
}
}
Here's how I used it:
// Decode the criteria
$criteria = json_decode($addl_criteria);
// Check input against criteria
foreach ($criteria as $item) {
// Criteria fails
if (!criteriaMet($input[$item->key)], $item->operator, $item->value)) {
return false;
}
}