How can PHP developers avoid common pitfalls when working with XML output in their code?

One common pitfall when working with XML output in PHP is not properly escaping special characters, which can lead to invalid XML and parsing errors. To avoid this issue, developers should use PHP's `htmlspecialchars()` function to encode special characters before outputting them in XML.

// Example code snippet to output XML with properly escaped special characters
$xml = new SimpleXMLElement('<data></data>');
$data = "Special characters like <>& should be properly escaped";
$xml->addChild('content', htmlspecialchars($data));
echo $xml->asXML();