PHP | DOMDocument createDocumentFragment() Function Last Updated : 20 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMDocument::createDocumentFragment() function is an inbuilt function in PHP which is used to create a new document fragment. Syntax: DOMDocumentFragment DOMDocument::createDocumentFragment( void ) Parameters: This function doesn’t accepts any parameters. Return Value: This function returns a new DOMDocumentFragment or FALSE if an error occurred. Below programs illustrate the DOMDocument::createDocumentFragment() function in PHP: Program 1: In this example we will create headings with fragments. php <?php // Create a new DOM Document $dom = new DOMDocument('1.0', 'iso-8859-1'); // Create a root element $dom->loadXML("<root/>"); // Create a Fragment $fragment = $dom->createDocumentFragment(); // Append the XML $fragment->appendXML( "<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3>"); // Append the fragment $dom->documentElement->appendChild($fragment); echo $dom->saveXML(); ?> Output: <?xml version="1.0"?> <root><h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3></root> Program 2: In this example we will create colored lines php <?php // Create a new DOM Document $dom = new DOMDocument('1.0', 'iso-8859-1'); // Create a root element $dom->loadXML("<root/>"); // Create a Fragment $fragment = $dom->createDocumentFragment(); // Colors $colors = ['red', 'green', 'blue']; for ($i = 0; $i < 3; $i++) { // Append the XML $fragment->appendXML( "<div style='color: $colors[$i]'>This is $colors[$i]</div>"); // Append the fragment $dom->documentElement->appendChild($fragment); } echo $dom->saveXML(); ?> Output: <?xml version="1.0"?> <root> <div style="color: red">This is red</div> <div style="color: green">This is green</div> <div style="color: blue">This is blue</div> </root> Reference: https://www.php.net/manual/en/domdocument.createdocumentfragment.php Comment More infoAdvertise with us Next Article PHP | DOMDocument createDocumentFragment() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMDocument createComment() Function The DOMDocument::createComment() function is an inbuilt function in PHP which is used to create a new instance of class createComment. Syntax: DOMComment DOMDocument::createComment( string $data ) Parameters: This function accepts single parameter $data which holds the content of the comment node. R 2 min read PHP | DOMDocument createElement() Function The DOMDocument::createElement() function is an inbuilt function in PHP which is used to create a new instance of class DOMElement. Syntax: DOMElement DOMDocument::createElement( string $name, string $value ) Parameters: This function accepts two parameters as mentioned above and described below: $n 2 min read PHP DOMDocument createElementNS() Function The DOMDocument::createElementNS() function is an inbuilt function in PHP that is used to create a new element node with an associated namespace. Syntax: DOMElement DOMDocument::createElementNS( string $namespaceURI, string $qualifiedName, string $value ) Parameters: This function accepts three para 2 min read PHP | DOMDocument createEntityReference() Function The DOMDocument::createEntityReference() function is an inbuilt function in PHP which is used to create a new instance of class DOMEntityReference. Syntax: DOMEntityReference DOMDocument::createEntityReference( string $name ) Parameters: This function accepts single parameter $name which holds the c 1 min read PHP | DOMDocument createCDATASection() Function The DOMDocument::createCDATASection() function is an inbuilt function in PHP which is used to create a new instance of class DOMCDATASection. Syntax: DOMCDATASection DOMDocument::createCDATASection( string $data ) Parameters: This function accepts single parameter $data which holds the content of th 1 min read Like