$doc = new DOMDocument(); $elem = $doc->createElement("div"); $attr = $doc->createAttribute("class"); $attr->value = "blue"; $elem->appendChild($attr); $doc->appendChild($elem);
$doc = new DomDocument('1.0'); $elem = $doc->createElement("item"); $attr = $doc->createAttribute("id"); $attr->value = "10"; $elem->appendChild($attr); $doc->appendChild($elem);In this example, a new `item` element is created and an attribute with the name `id` and a value of `10` is added to it. The `createAttribute()` method is part of the PHP DOM extension, which is included with PHP by default.