I just encounter a syntax expression while working with laravel model.
public function address($type)
{
return $this->address[$type] ?? [];
}
I don't understand how does this
return $this->address[$type] ?? [];
Work?
I just encounter a syntax expression while working with laravel model.
public function address($type)
{
return $this->address[$type] ?? [];
}
I don't understand how does this
return $this->address[$type] ?? [];
Work?
It's the null coalescing operation (see more at https://en.wikipedia.org/wiki/Null_coalescing_operator#PHP). It's a syntactic shortcut for isset
, really:
return $this->address[$type] ?? [];
can be read as:
if (isset($this->address[$type])) {
return $this->address[$type];
} else {
return [];
}