Sunday, December 14, 2025
HomeLanguagesPHP | DOMComment __construct() Function

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):




<?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();
?>


Output:

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

Program 2 (Using comments with elements):




<?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();
?>


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

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

Most Popular

Dominic
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11953 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6951 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS