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?