當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP DOMComment __construct()用法及代碼示例

DOMComment::__construct()函數是PHP中的內置函數,可創建新的DOMComment對象。該對象是隻讀的,可以附加到文檔中

用法:

public DOMComment::__construct( string $value)

參數:該函數接受一個包含注釋的單個參數$value。


下麵給出的程序說明了PHP中的DOMComment::__construct()函數:

計劃1(簡單評論):

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

輸出:

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

計劃2(在元素中使用注釋):

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

輸出:

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

參考: https://www.php.net/manual/en/domcomment.construct.php



相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | DOMComment __construct() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。