So I have something like the following:
$a = 3;
$b = 4;
$c = 5;
$d = 6;
and I run a comparison like
if($a>$b || $c>$d
-
No, it is not possible.
讨论(0)
-
nope. there is no way to do this in php.
讨论(0)
-
For future searchers, here's a function I came up with when I had a requirement 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;
break;
case '<=':
return $value1 <= $value2;
break;
case '>':
return $value1 > $value2;
break;
case '>=':
return $value1 >= $value2;
break;
case '==':
return $value1 == $value2;
break;
case '!=':
return $value1 != $value2;
break;
default:
return false;
}
return false;
}
(edit) 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;
}
}
讨论(0)
-
Please use this code for change the string operator to convert in actual format
<?php
$a = 3;
$b = 4;
$c = 5;
$d = 6;
$e='&&';
$lt='<';
$gt='>';
if(eval('return '.$a.$lt.$b.$e.$c.$gt.$d.';')){
echo "yes";
}else{
echo "No";
}
讨论(0)
-
You could use eval, but that you could easily end up exposing your site to all sorts of code injection attacks if you're not very careful.
A safer solution would be to match the proposed operator against a predefined white list and then call a corresponding bit if code with the operator hard - coded.
C.
讨论(0)
- 热议问题