PHP | DOMComment __construct() Function

The DOMComment::__construct() function is an inbuilt function in PHP which creates a new DOMComment object. This object is read only and can be appended to a document

Syntax:

public DOMComment::__construct( string $value)

Parameters: This function accepts a single parameter $value which holds the comment.

Below given programs illustrate the DOMComment::__construct() function in PHP:

Program 1 (Simple comment):



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOM Document
$dom = new DOMDocument('1.0', 'iso-8859-1');
  
// Create a h1 element
$element = $dom->appendChild(new DOMElement('h1'));
  
// Create a DOMComment
$comment = $element->appendChild(
        new DOMComment('This line is a comment'));
  
echo $dom->saveXML();
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<h1><!--This line is a comment--></h1>

Program 2 (Using comments with elements):

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOM Document
$dom = new DOMDocument('1.0', 'iso-8859-1');
  
// Create a h1 element
$element = $dom->appendChild(new DOMElement('h1'));
  
// Create a DOMCdataSection 
$comment = $element->appendChild(new DOMComment(
        'This line is a comment about content'));
  
// Create a div element
$element = $element->appendChild(new DOMElement(
          'div', 'This is the actual content'));
  
echo $dom->saveXML();
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<h1><!--This line is a comment about content-->
<div>This is the actual content</div></h1>

Reference: https://www.php.net/manual/en/domcomment.construct.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.