PHP DOMDocument saveHTMLFile() Function Last Updated : 03 Apr, 2023 Comments Improve Suggest changes Like Article Like Report The DOMDocument::saveHTMLFile() function is an inbuilt function in PHP that is used to create an HTML document from the DOM representation. This function is used after creating the dom document. Syntax: int DOMDocument::saveHTMLFile( string $filename ) Parameters: This function accepts a single parameter $filename which holds the path to save the HTML document. Return Value: This function returns the number of bytes on success or FALSE on failure. Below example illustrates the DOMDocument::saveHTMLFile() function in PHP: Example: php <?php // Create a new DOMDocument $domDocument = new DOMDocument('1.0'); // Create a root element $root = $domDocument->createElement('html'); // Append the element to the document as root element $root = $domDocument->appendChild($root); // Create a head element $head = $domDocument->createElement('head'); // Append the element to the document // as child element $head = $root->appendChild($head); // Create a title element $title = $domDocument->createElement('title'); // Append the element to the document // as child element $title = $head->appendChild($title); // Create a text node $text = $domDocument->createTextNode( 'DOMDocument::saveHTML() function'); // Add the text node to the title element $text = $title->appendChild($text); // Create a body element $body = $domDocument->createElement('body'); // Append the element to the document // as child element $body = $root->appendChild($body); // Create a heading element $h1 = $domDocument->createElement('h1'); // Append the element to the document $h1 = $body->appendChild($h1); // Create a text node $text = $domDocument->createTextNode('GeeksforGeeks'); // Add the text node to the heading element $text = $h1->appendChild($text); // Create a heading element $h2 = $domDocument->createElement('h2'); // Append the element to the document $h2 = $body->appendChild($h2); // Create a text node $text = $domDocument->createTextNode( 'DOMDocument::saveHTML() function'); // Add the text node to the heading element $text = $h2->appendChild($text); // Use saveHTMLFile() function to save // an HTML document $domDocument->saveHTMLFile('gfg.html'); echo "HTML file saved successfully"; ?> Output: HTML file saved successfully The content of the saved HTML file gfg.html: html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>DOMDocument::saveHTML() function</title> </head> <body> <h1>GeeksforGeeks</h1> <h2>DOMDocument::saveHTML() function</h2> </body> </html> Reference: https://www.php.net/manual/en/domdocument.savehtmlfile.php Comment More infoAdvertise with us Next Article PHP DOMDocument saveHTMLFile() Function jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP DOMDocument saveHTML() Function The DOMDocument::saveHTML() function is an inbuilt function in PHP that is used to create an HTML document from the DOM representation. This function is used after building the dom document from scratch. Syntax: string DOMDocument::saveHTML( DOMNode $node = NULL ) Parameters: This function accepts s 2 min read PHP | DOMDocument saveXML() Function The DOMDocument::saveXML() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after building a new dom document from scratch. Syntax: string DOMDocument::saveXML( DOMNode $node, int $options = 0 ) Parameters: This functio 2 min read PHP | DOMDocument save() Function The DOMDocument::save() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after creating the new dom document from scratch. Syntax: int DOMDocument::save( string $filename, int $options = 0 ) Parameters: This function ac 2 min read PHP | DOMDocument validate() Function The DOMDocument::validate() function is an inbuilt function in PHP which is used to validate the document based on its DTD (Document Type Definition). DTD defines the rules or structure to be followed by the XML file and if a XML document doesn't follows this format then this function will return fa 2 min read PHP | DOMDocument loadHTMLFile() Function The DOMDocument::loadHTMLFile() function is an inbuilt function in PHP which is used to load HTML from a file. Syntax: bool DOMDocument::loadHTMLFile( string $filename, int $options = 0 ) Parameters: This function accepts two parameters as mentioned above and described below: $filename: This paramet 2 min read Like