代码之家  ›  专栏  ›  技术社区  ›  Fil

如何返回$this->address[$type]??[];工作?

  •  0
  • Fil  · 技术社区  · 7 年前

    我在使用laravel模型时遇到了一个语法表达式。

      public function address($type)
        {
            return $this->address[$type] ?? [];
        }
    

    我不明白这是怎么回事

     return $this->address[$type] ?? [];
    

    工作?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Adam Wright    7 年前

    这是空合并操作(参见 https://en.wikipedia.org/wiki/Null_coalescing_operator#PHP ). 它是 isset ,真的:

    return $this->address[$type] ?? [];
    

    可以理解为:

    if (isset($this->address[$type])) {
      return $this->address[$type];
    } else {
      return [];
    }
    
    推荐文章