PHP | DOMComment __construct() Function
Last Updated :
14 Feb, 2020
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
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$element = $dom ->appendChild( new DOMElement( 'h1' ));
$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
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$element = $dom ->appendChild( new DOMElement( 'h1' ));
$comment = $element ->appendChild( new DOMComment(
'This line is a comment about content' ));
$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
Share your thoughts in the comments
Please Login to comment...