Is there a better method for covering errors in my case? I am looking for best practice for now and future instances.Foreseen errors that could arise:No attributes at allSome attributes could be missingAttribute value could be empty// converting dom into array to use later$fields = $dom->getElementsByTagName($field);$arr['field'] = array();foreach ($fields as $field) {    $attributes = isset($field->attributes) ? $field->attributes : NULL;    if (!empty($attributes)) {        // the method getNamedItem($string) returns NULL if not found        $name = !empty($attributes->getNamedItem('name')) ? $attributes->getNamedItem('name')->nodeValue : NULL;        $id = !empty($attributes->getNamedItem('id')) ? $attributes->getNamedItem('id')->nodeValue : NULL;        $field_name = !empty($attributes->getNamedItem('field_name')) ? $attributes->getNamedItem('field_name')->nodeValue : NULL;        if (!empty($name) && !empty($id) && !empty($field_name)) {            $arr['field'][$name][$id][$field_name] = $field->nodeValue;        }    }}
Checking empty object
php;error handling
I think it is cleaner to handle special cases early; your code winds up less indented.// converting dom into array to use later$fields = $dom->getElementsByTagName($field);$arr['field'] = array();foreach ($fields as $field) {  $attributes = isset($field->attributes) ? $field->attributes : NULL;  if (empty($attributes)) {    continue;  }  $name = !empty($attributes->getNamedItem('name')) ? $attributes->getNamedItem('name')->nodeValue : NULL;  $id = !empty($attributes->getNamedItem('id')) ? $attributes->getNamedItem('id')->nodeValue : NULL;  $field_name = !empty($attributes->getNamedItem('field_name')) ? $attributes->getNamedItem('field_name')->nodeValue : NULL;  if (empty($name) || empty($id) || empty($field_name)) {    continue;  }  $arr['field'][$name][$id][$field_name] = $field->nodeValue;}This looks less complicated, at least to me, but it does exactly the same work.You could also introduce a method to get the nodeValue or NULL, given $attributes and a name.// converting dom into array to use later$fields = $dom->getElementsByTagName($field);$arr['field'] = array();foreach ($fields as $field) {  $attributes = isset($field->attributes) ? $field->attributes : NULL;  if (empty($attributes)) {    continue;  }  $name       = nodeIfPresent($attributes, 'name');  $id         = nodeIfPresent($attributes, 'id');  $field_name = nodeIfPresent($attributes, 'field_name');  if (empty($name) || empty($id) || empty($field_name)) {    continue;  }  $arr['field'][$name][$id][$field_name] = $field->nodeValue;}