$doc = new DOMDocument(); $body = $doc->getElementsByTagName('body')->item(0); $paragraph = $doc->createElement('p', 'Hello World!'); $body->appendChild($paragraph);
$div = $doc->createElement('div'); $div->setAttribute('class', 'container'); $body->appendChild($div);This code creates a new DOMElement object with the tag name "div" and sets its class attribute to "container". It is then appended to the body of the HTML document. In summary, the createElement method of the PHP DOMDocument class is a powerful tool for PHP developers to create new HTML elements and manipulate existing ones in a DOM document. It is part of the PHP DOM API library, which comes bundled with PHP.