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


PHP DOMNode hasChildNodes()用法及代碼示例

DOMNode::hasChildNodes()函數是PHP中的內置函數,用於檢查節點是否有子級。

用法:

bool DOMNode::hasChildNodes( void )

參數:此函數不接受任何參數。



返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。

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

程序1:

<?php 
// Create a new DOMDocument 
$dom = new DOMDocument(); 
  
// Create a paragraph element 
$element = $dom->createElement('p', 'GeeksforGeeks!'); 
  
// Append the child 
$dom->appendChild($element); 
  
// Check if child nodes are there 
if ($dom->hasChildNodes()) { 
    echo "Yes, child nodes are there."; 
} 
?>

輸出:

Yes, child nodes are there.

程序2:

<?php 
// Create a new DOMDocument instance and keep it empty 
$dom = new DOMDocument(); 
  
// Check if child nodes are not there 
if (!$dom->hasChildNodes()) { 
    echo "No, child nodes are not there."; 
} 
?>

輸出:

No, child nodes are not there.

參考: https://www.php.net/manual/en/domnode.haschildnodes.php




相關用法


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