PHP - warning - Undefined property: stdClass - fix?

后端 未结 9 1083
梦毁少年i
梦毁少年i 2020-11-30 05:22

I get this warning in my error logs and wanted to know how to correct this issues in my code.

Warning: PHP Notice: Undefined property: stdClass::$records in script

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 06:11

    isset() is fine for top level, but empty() is much more useful to find whether nested values are set. Eg:

    if(isset($json['foo'] && isset($json['foo']['bar'])) {
        $value = $json['foo']['bar']
    }
    

    Or:

    if (!empty($json['foo']['bar']) {
        $value = $json['foo']['bar']
    }
    

提交回复
热议问题