Using a variable as an operator

后端 未结 11 1421
滥情空心
滥情空心 2020-12-06 09:31

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         


        
相关标签:
11条回答
  • 2020-12-06 09:35
     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;
            }
        }
    
    0 讨论(0)
  • 2020-12-06 09:37

    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)))
     ....
    
    0 讨论(0)
  • 2020-12-06 09:38

    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.

    0 讨论(0)
  • 2020-12-06 09:44

    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;
    }
    
    0 讨论(0)
  • 2020-12-06 09:44

    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';
    };
    
    0 讨论(0)
  • 2020-12-06 09:46

    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
    }
    
    0 讨论(0)
提交回复
热议问题