36

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

10 Answers 10

28

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;
}
Sign up to request clarification or add additional context in comments.

Comments

13

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

Comments

12

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

Comments

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

Comments

3

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
}

Comments

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

1 Comment

You should give the details and explanation of code what you have written in answer.
2

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.

Comments

0

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)))
 ....

Comments

0

No, it is not possible.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.