Using a variable as an operator

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) { echo 'yes'; };

That all works just fine. Is it possible to use a variable in place of the operator?
Something like:

$e = ||;

Which I could then use as

if($a>$b $e $c>$d) { echo 'yes'; };

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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;
}

Solution 2

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';
};

Solution 3

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;
    }
}

Solution 4

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";
  }

Solution 5

 public function checkOperator($value1, $operator, $value2)
    {
        switch ($operator) {
            case '%': // Percentage
                return $value1 % $value2;
            case '+': // Sum
                return $value1 + $value2;
            case '-': // subtraction
                return $value1 - $value2;
            case '*': // Multiplication
                return $value1 * $value2;
            case '/': // Divided
                return $value1 / $value2;
            case '': // Greater than
                return $value1 > $value2;
            case '>=': // Greater than or equal to
                return $value1 >= $value2;
            case '==': // Equal
                return $value1 == $value2;
            case '===': // Identical
                return $value1 === $value2;
            case '!==': // Not Identical
                return $value1 !== $value2;
            case '!=': // Not equal
            case '': // Not equal
                return $value1 != $value2;
            case '||': // Or
            case 'or': // Or
                return $value1 || $value2;
            case '&&': // And
            case 'and': // And
                return $value1 && $value2;
            case 'xor': // Or
                return $value1 xor $value2;
            default:
                return false;
        }
    }

Solution 6

Just to make the list complete this is the function I use. It has all the operators. Its better not to use eval(). This will be much quicker and safer.

/* -------------------------------------------------------------------------
 * checks 2 values with operator
 * you can use logical operators als well
 * returns FALSE or TRUE
 */
function checkOperator($value1, $operator, $value2) {
    switch ($operator) {
        case '<': // Less than
            return $value1 < $value2;
        case '<=': // Less than or equal to
            return $value1 <= $value2;
        case '>': // Greater than
            return $value1 > $value2;
        case '>=': // Greater than or equal to
            return $value1 >= $value2;
        case '==': // Equal
            return $value1 == $value2;
        case '===': // Identical
            return $value1 === $value2;
        case '!==': // Not Identical
            return $value1 !== $value2;
        case '!=': // Not equal
        case '<>': // Not equal
            return $value1 != $value2;
        case '||': // Or
        case 'or': // Or
           return $value1 || $value2;
        case '&&': // And
        case 'and': // And
           return $value1 && $value2;
        case 'xor': // Or
           return $value1 xor $value2;  
        default:
            return FALSE;
    } // end switch

To call it:


$value1 = 12;
$operator = '>';
$value2 = 13;
if (checkOperator($value1, $operator, $value2)) {
    ... its true
} else {
    ... its not true
}

Solution 7

eval is often perfectly legitimate to use in such cases, if you don’t use arbitrary user input or can whitelist simple math expressions:

 $expr = "$var1 $op $var2";
 $rx_math = '/^
      \d+(\.\d+)? \s*              # numeric
      ([-+\/*<>^%]|>=|<=|==)       # operator
      \s* \d+(\.\d+)?              # numeric
 $/x';
 if (preg_match($rx_math, $expr)) {
      eval("\$result = $expr;");
 }

Writing your own math parser is fun of course. But slightly misguided in the context of scripting languages, where it’s a built-in feature anyway.

Solution 8

Nope, there is no way to re-define operators (or use variable operators) in PHP AFAIK.

Short of using eval(), the closest I can think of is creating a function:

function my_operator ($cond1, $cond2)
 {
   if ( ....  ) 
     return ($cond1 || $cond2);
   else
     return ($cond1 && $cond2);

 }

if (my_operator(($a > $b), ($c > $d)))
 ....

Solution 9

No, it is not possible.

Solution 10

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply