Operator null coalescing, ternary and empty

-1

Is there any alternative to !empty($foo) ? $foo : $bar ?

Since $foo ?? $bar , accepts false and $foo ?: $bar is not used to check array indices or unset variables.

I'll give you an example using empty :

public function __construct($params){
    $this->foo = !empty($params['foo']) ? $params['foo'] : null;
}

That's equivalent to using here isset :

public function __construct($params){
    $this->foo = isset($params['foo']) && $params['foo'] != false ? $params['foo'] : null;
}

But if it were:

public function __construct($params){
    $this->foo = isset($params['foo']) ? $params['foo'] : null;
}

I could simply replace it with:

public function __construct($params){
    $this->foo = $params['foo'] ?? null;
}

Or if it were:

public function __construct($params){
    $this->foo = $params['foo'] != false ? $params['foo'] : null;
}

I could replace with:

public function __construct($params){
    $this->foo = $params['foo'] ?: null;
}

The question is whether there is a direct substitute for the first example, but it does not seem to exist. It could be something like $foo ?! $bar

    
asked by anonymous 16.12.2018 / 16:42

0 answers