What are common issues with using print or echo in PHP to output XML format?
Common issues with using print or echo to output XML format in PHP include syntax errors, improper formatting, and difficulty in maintaining the structure of the XML document. To solve these issues, it is recommended to use PHP's built-in SimpleXMLElement class to create and manipulate XML data, ensuring proper structure and escaping of special characters.
// Create a SimpleXMLElement object to build the XML structure
$xml = new SimpleXMLElement('<root></root>');
// Add data to the XML structure
$xml->addChild('name', 'John Doe');
$xml->addChild('age', 30);
// Output the XML content with proper formatting
header('Content-Type: text/xml');
echo $xml->asXML();