In plain English: json_decode() returns null when the JSON is invalid.
If you immediately read a property or array key from that result, PHP tries to
read from null and can fail.
Why it matters: A malformed response or request body can cause a warning, an exception, or a confusing “property on null” error instead of a clear validation message.
return json_decode($json)->name;
return json_decode($json, true)[0];
$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
return $data['name'];
JSON_THROW_ON_ERROR makes invalid JSON throw a JsonException that you can
catch. If you do not want exceptions, check the result first:
$data = json_decode($json, true);
if (!is_array($data)) {
return null;
}
return $data['name'] ?? null;
json_decode($json)->propertyjson_decode($json)->method()json_decode($json)[$key]It also accepts direct accesses guarded by ??, isset(), or empty(), and
calls that use JSON_THROW_ON_ERROR in a positional or named flags argument.
??, isset(), or empty().JSON_THROW_ON_ERROR.JSON_THROW_ON_ERROR and catch JsonException.is_array() or is_object() before reading the result.?? or isset() for optional keys.E0052 has no configuration options and is enabled by default.