views:

131

answers:

7

Hi All

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't work):

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

if(isAnd(1, 1, '===')) echo 'worked';

Thanks in advance.

A: 

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.

Col. Shrapnel
+1  A: 

If you absolutely insist you can use eval.

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

But I wouldn't recommend it.

Michael Krelin - hacker
+1 - but some explanation of why you don't recommend it may be of value to the readers
symcbean
A: 

As far as I know it is not possible and since there is no reference about callback on operators in PHP documentation, http://www.php.net/manual/en/language.operators.php

instead of using eval, I would redefine each operators in global functions and use php callbacks http://stackoverflow.com/questions/48947/how-do-i-implement-a-callback-in-php

snowflake
+1  A: 

How about a small class:

class compare
{
  function is($op1,$op2,$c)
  {
     $meth = array('===' => 'type_equal', '<' => 'less_than');
     if($method = $meth[$c]) {
        return $this->$method($op1,$op2);
     }
     return null; // or throw excp.
  }
  function type_equal($op1,$op2)
  {
      return $op1 === $op2;
  }
  function less_than($op1,$op2)
  {
      return $op1 < $op2;
  }
}
giftnuss
I didn't even think about using a class, too early in the morning maybe? Thanks.
BenTheDesigner
Why does it need to be a class?
symcbean
Before 5.3 this is the best choice, with namespaces and real callbacks other good solutions are possible. But a class is probably the simplest one.
giftnuss
It takes some time to understand your question. You are right, a class is not a required for this, but it is a common practice in PHP.
giftnuss
+2  A: 

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?

deceze
Also see related http://stackoverflow.com/questions/1960509/isset-and-empty-make-code-ugly/1960588#1960588
deceze
A: 

As Michael Krelin suggests you could use eval - but that potentially enables a lot of code injection attacks.

You can't substitute a variable for an operator - but you can substitute a variable for a function:

function is_equal($a, $b) {
  return $a==$b;
} 
function is_same($a, $b) {
  return $a===$b;
}
function is_greater_than($a, $b)
....

$compare='is_equal';
if ($compare($a, $b)) {
   ....

C.

symcbean
A: 

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

alokin