Tristan
Tristan

Reputation: 1685

In PHP, is it possible to replicate the way that isset() suppresses warnings for undefined variables in its arguments?

With the isset() function, you can pass undefined variables to its arguments without getting a warning. I was wondering if it is possible to replicate this functionality somehow.

For instance, I tried to make a function that combines isset($value) && is_array($value).

However, if I try to test it with an undefined variable, I get a "Warning: Undefined variable $somevar" warning.

function isset_array(mixed $value): bool
{
    if (isset($value) && is_array($value)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

var_dump(isset_array($somevar));

I don't get the same warning with isset().

var_dump(isset($somevar));

I assume that this is some hardcoded behavior in isset().

Is it possible to replicate this behavior? Or is this some hardcoded PHP functionality that you can't mimic?

Upvotes: 0

Views: 73

Answers (0)

Related Questions