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
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
$domComment = $domDocument->createComment('GeeksforGeeks');
$domDocument->appendChild($domComment);
echo $domDocument->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<!--GeeksforGeeks-->
Program 2:
<?php
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
$domComment1 = $domDocument->createComment('Starting XML document file');
$domComment2 = $domDocument->createComment('Ending XML document file');
$domElement1 = $domDocument->createElement('organization');
$domElement2 = $domDocument->createElement('name', 'GeeksforGeeks');
$domElement3 = $domDocument->createElement('address', 'Noida');
$domElement4 = $domDocument->createElement('email', 'abc@geeksforgeeks.org');
$domDocument->appendChild($domComment1);
$domDocument->appendChild($domElement1);
$domElement1->appendChild($domElement2);
$domElement1->appendChild($domElement3);
$domElement1->appendChild($domElement4);
$domDocument->appendChild($domComment2);
echo $domDocument->saveXML();
?>
|
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!