Dynamic Comparison Operators in PHP

后端 未结 11 662
挽巷
挽巷 2020-12-03 11:24

Is it possible, in any way, to pass comparison operators as variables to a function? I am looking at producing some convenience functions, for example (and I know this won\'

相关标签:
11条回答
  • 2020-12-03 11:48

    If you absolutely insist you can use eval.

    if(isset($var) && eval("return \$var $operator \$value"))
        return true;
    

    But I wouldn't recommend it.

    0 讨论(0)
  • 2020-12-03 11:49

    No, it's impossible. You can use conditional operators instead, but it will be much,much better if you redesign your application to make such a dynamic comparison unnecessary.

    0 讨论(0)
  • 2020-12-03 11:53

    How about this one?

    function num_cond ($var1, $op, $var2) {
    
        switch ($op) {
            case "=":  return $var1 == $var2;
            case "!=": return $var1 != $var2;
            case ">=": return $var1 >= $var2;
            case "<=": return $var1 <= $var2;
            case ">":  return $var1 >  $var2;
            case "<":  return $var1 <  $var2;
        default:       return true;
        }   
    }
    

    Test:

    $ops = array( "=", "!=", ">=", "<=", ">", "<" );
    $v1 = 1; $v2 = 5;
    
    foreach ($ops as $op) {
        if (num_cond($v1, $op, $v2)) echo "True  ($v1 $op $v2)\n"; else echo "False ($v1 $op $v2)\n";
    }
    
    0 讨论(0)
  • 2020-12-03 11:54

    The bigger problem is that this function is pretty pointless. Let's replace that with a real (hypothetically working) example:

    function isAnd($var, $value, $operator = '==') {
        return isset($var) && $var $operator $value;
    }
    
    isAnd($foo, 1, '===');
    

    In this example $foo is not set. You'll get an error because you're trying to pass a non-existent variable ($foo) to a function (isAnd). So, you will need to test $foo for isset before calling isAnd:

    isset($foo) && isAnd($foo, 1, '===');
    

    So, any variable that ever enters the isAnd function is definitely set. You don't need to test for it inside the function. So the whole exercise is pretty pointless.

    What may be confusing is that isset() and empty() don't have this limitation, i.e. you can pass a non-existent variable to them without error. The thing is though, these are not normal functions, they're special language constructs (that happen to look like functions; blame PHP). Unfortunately you can not make these kinds of constructs, parameters for your functions always need to exist.

    You should just get used to writing isset($foo) && $foo === 1. With properly structured code, you can reduce this to a minimum by always declaring all variables you're going to use, which is good practice anyway.

    For the dynamic operator... you'll need some form of if ... else somewhere to decide which operator to use anyway. Instead of setting the operator variable and then evaluating it, isn't it easier to do the evaluation right there?

    0 讨论(0)
  • 2020-12-03 11:55

    You can also use version_compare() function, as you can pass operator which will be used for comparison as third argument.

    0 讨论(0)
  • 2020-12-03 11:58

    Here is a simple solution which should work for almost all the operators

    Eg.

    $b = 10;
    $c = '+';
    $p = $a . $c. $b; // Forming a String equation
    $p = eval('return '.$p.';'); // Evaluating the Equation
    echo $p;
    

    Output:

    15
    

    Another example with comparison operator:

    $b = 10;
    $c = '==';
    $p = $a . $c. $b;
    $p = eval('return '.$p.';');
    echo $p;
    

    Output:

    false
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题