Search code examples
phpelementskipxmlwriter

PHP XMLWriter skip empty tags


$xmlF->writeElement($fieldName, $fieldVal);

Currently, when $fieldVal is empty, above will output:

<FieldName/>

Is it possible to skip element entirely (other than with if's) when its value is empty?


Solution

  • You can create condition for that:

    if (!empty($fieldVal)) {
        $xmlF->writeElement($fieldName, $fieldVal);
    }