multiple isset OR conditions

Viewed 547

isset can be used to check if multiple variables are ALL set:

if (isset($var1, $var2, $var3)) {...}

But what if I want to check if ANY of the variables are set?

if (isset($var1) || isset($var2) || isset($var3)) {...}

Given that using @ for error suppression is considered bad practice, is there a better way than this?

function orset()
{
    foreach (func_get_args() as $arg) {
        if (isset($arg)) return true;
    }
    return false;
}

if (orset(@$var1, @$var2, @$var3)) {...}
3 Answers

I came up with new idea of checking whether any of the variables is set. However it's not possible as deceze mentioned, but this way is easier to list all variables you want to check. But still it's not very elegant way to achieve it, because there is no nice way to do it.

Solution is to chain Null Coalescing Operator. It's important to return null at the end of chain because other way it will show Notice for non-existing array's element.

$var3 = 'test';
$var4['4'] = false;
$var4['3'] = null;

var_dump(!is_null($var1 ?? $var2 ?? $var3 ?? $var4['4'] ?? null)); //true
var_dump(!is_null($var1 ?? $var2 ?? $var3 ?? null)); //true
var_dump(!is_null($var1 ?? $var2 ?? null)); //false
var_dump(!is_null($var1 ?? $var2 ?? $var4['3'] ?? null)); //false
var_dump(!is_null($var1 ?? $var2 ?? $var4['5'] ?? null)); //false

isset checks whether a variable is null while suppressing a notice should that variable not exist at all.

There is no way to create a user defined function that would work like isset. A user defined function can only accept values, not "variables". The value must be resolved at the caller side, so a non-existent variable will always generate a notice there and there's nothing your function can do to suppress it. isset is a "language construct", which is more like an operator than a function, and there's no way to imitate what it does in userland code. In certain special cases you may be able to implement some workaround, e.g. using global variables, but those will fail in other circumstances, e.g. when you're not working with global variables.

Fundamentally you shouldn't ever have to deal with non-existent variables all that much in the first place. Your code is entirely in control what variables it creates, so if you define your variables then you don't need to check whether they exist or not. Anything dynamic should be passed as arrays or objects, e.g. user input via $_GET or $_POST or other data structures which can be passed around as objects. Then you're not talking about isset anymore, but about testing the existence of keys in an array for example, which can be simplified, e.g.:

if (array_intersect_key($array, array_flip(['foo', 'bar', 'baz']))) {
    echo 'At least one of those keys was in $array';
}

So, avoid getting into a situation where you need to check whether certain variables are set in the first place. If you are inevitably in that situation, then there's no real workaround for isset || isset || ....

I am not entirely sure if this is the best way to approach this ... You could pass only variable names to your function and not the variables.

//$var1 = 1;
//$var2 = 2;
//$var3 = 3;

function orset()
{
    foreach (func_get_args() as $arg) {       
        global ${$arg};        
        if (isset(${$arg})) {
            return true;
        }
    }

    return false;
}

if (orset('var1', 'var2', 'var3')) {
    echo "ok"; 
}

Why do you want to suppress errors? Passing an undefined variable to a function should produce a notice. If you forget about supressing errors (which you should) you could use:

function orset()
{
    foreach (func_get_args() as $arg) {       
        global $arg;        
        if (isset($arg)) {
            return true;
        }
    }

    return false;
}

if (orset($var1, $var2, $var3)) {
    echo "ok"; 
}

It's just complicating stuff when you can simply use if (isset($var1) || isset($var) ....

Related