DOM will do it automatically for you
$dom = new DOMDocument;
$dom->appendChild($dom->createElement('foo'));
echo $dom->saveXml();
will give default
<?xml version="1.0"?>
<foo/>
if you do not
$dom = new DOMDocument;
$dom->appendChild($dom->createElement('foo'));
echo $dom->saveXml($dom, LIBXML_NOEMPTYTAG);
which would then give
<?xml version="1.0" encoding="UTF-8"?>
<foo></foo>
source
share