How can attributes be added to child elements in PHP when creating XML output?

To add attributes to child elements in PHP when creating XML output, you can use the `setAttribute()` method on the child element node. This method allows you to specify the attribute name and value for the child element. This way, you can dynamically add attributes to specific child elements in your XML output.

// Create a new XML document
$xml = new DOMDocument();

// Create a parent element
$parentElement = $xml->createElement('parent');
$xml->appendChild($parentElement);

// Create a child element
$childElement = $xml->createElement('child');
$parentElement->appendChild($childElement);

// Add attribute to the child element
$childElement->setAttribute('attribute_name', 'attribute_value');

// Output the XML
echo $xml->saveXML();