What are the potential pitfalls of using namespaces in PHP when creating XML documents?

When creating XML documents in PHP with namespaces, a potential pitfall is forgetting to properly define and use the namespace prefixes throughout the document. This can lead to errors when parsing the XML or when trying to access elements within the document. To avoid this issue, always define the namespaces at the beginning of the document and use the correct prefixes when referencing elements.

<?php
$xml = new DOMDocument('1.0', 'utf-8');
$root = $xml->createElement('root');
$root->setAttribute('xmlns:ns', 'http://example.com/ns');
$xml->appendChild($root);

$child = $xml->createElement('ns:child');
$root->appendChild($child);

echo $xml->saveXML();