当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP DOMDocument createComment()用法及代码示例


DOMDocument::createComment()函数是PHP中的内置函数,用于创建类createComment的新实例。

用法:

DOMComment DOMDocument::createComment( string $data )

参数:该函数接受包含注释节点内容的单个参数$data。


返回值:如果成功,此函数返回新的DOMComment对象,如果失败,则返回FALSE。

以下示例程序旨在说明PHP中的DOMDocument::createComment()函数:

程序1:

<?php 
  
// Create a new DOMDocument 
$domDocument = new DOMDocument('1.0', 'iso-8859-1'); 
  
// Use createComment() function to add a new comment node 
$domComment = $domDocument->createComment('GeeksforGeeks'); 
  
// Append element to the document 
$domDocument->appendChild($domComment); 
  
// Save the XML file and display it 
echo $domDocument->saveXML(); 
  
?>
输出:
<?xml version="1.0" encoding="iso-8859-1"?>
<!--GeeksforGeeks-->

程序2:

<?php 
  
// Create a new DOMDocument 
$domDocument = new DOMDocument('1.0', 'iso-8859-1'); 
  
// Use createComment() function to add a new comment node 
$domComment1 = $domDocument->createComment('Starting XML document file'); 
$domComment2 = $domDocument->createComment('Ending XML document file'); 
  
// Use createElement() function to add a new element node 
$domElement1 = $domDocument->createElement('organization'); 
$domElement2 = $domDocument->createElement('name', 'GeeksforGeeks'); 
$domElement3 = $domDocument->createElement('address', 'Noida'); 
$domElement4 = $domDocument->createElement('email', 'abc@geeksforgeeks.org'); 
  
// Append element to the document 
$domDocument->appendChild($domComment1); 
  
$domDocument->appendChild($domElement1); 
$domElement1->appendChild($domElement2); 
$domElement1->appendChild($domElement3); 
$domElement1->appendChild($domElement4); 
  
$domDocument->appendChild($domComment2); 
  
// Save the XML file and display it 
echo $domDocument->saveXML(); 
  
?>
输出:
<?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-->

参考: https://www.php.net/manual/en/domdocument.createcomment.php



相关用法


注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 PHP | DOMDocument createComment() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。