Tuesday, June 9, 2026
HomeLanguagesPHP | DOMDocument createComment() Function

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:




<?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('neveropen');
  
// Append element to the document
$domDocument->appendChild($domComment);
  
// Save the XML file and display it
echo $domDocument->saveXML();
  
?>


Output:

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

Program 2:




<?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', 'neveropen');
$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();
  
?>


Output:

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

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS