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.

Return Value: This function returns the new DOMComment object on success or FALSE on failure.

Below programs illustrate the DOMDocument::createComment() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createComment() function to add a new comment node
$domComment = $domDocument->createComment('GeeksforGeeks');
  
// Append element to the document
$domDocument->appendChild($domComment);
  
// Save the XML file and display it
echo $domDocument->saveXML();
  
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<!--GeeksforGeeks-->

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createComment() function to add a new comment node
$domComment1 = $domDocument->createComment('Starting XML document file');
$domComment2 = $domDocument->createComment('Ending XML document file');
  
// Use createElement() function to add a new element node
$domElement1 = $domDocument->createElement('organization');
$domElement2 = $domDocument->createElement('name', 'GeeksforGeeks');
$domElement3 = $domDocument->createElement('address', 'Noida');
$domElement4 = $domDocument->createElement('email', 'abc@geeksforgeeks.org');
  
// Append element to the document
$domDocument->appendChild($domComment1);
  
$domDocument->appendChild($domElement1);
$domElement1->appendChild($domElement2);
$domElement1->appendChild($domElement3);
$domElement1->appendChild($domElement4);
  
$domDocument->appendChild($domComment2);
  
// Save the XML file and display it
echo $domDocument->saveXML();
  
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<!--Starting XML document file-->
<organization>
    <name>GeeksforGeeks</name>
    <address>Noida</address>
    <email>abc@geeksforgeeks.org</email>
</organization>
<!--Ending XML document file-->

Reference: https://www.php.net/manual/en/domdocument.createcomment.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.