If you want to add the id attribute to your html element so that it <p id="frob">...does not look like you are using it setIdAttribute(), it declares that the attribute $namecan be used as a unique identifier for this element - as an alternative / adding attribute id. Use setAttribute()as follows:
$dom = new DOMDocument();
$dom->loadHTML('<html><body><p>FROB</p></body></html>');
$dom->getElementsByTagName('p')->item(0)->setAttribute('id', 'XXX');
print $dom->saveHTML();
source
share