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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。