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


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

DOMNode::hasAttributes()函數是PHP中的內置函數,用於檢查節點是否具有屬性。

用法:

bool DOMNode::hasAttributes( void )

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


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

以下示例說明了PHP中的DOMNode::hasAttributes()函數:

範例1:

<?php 
   
// Create a new DOMDocument 
$dom = new DOMDocument(); 
    
// Create a paragraph element 
$element = $dom->createElement('p', 
      'This is the paragraph element!'); 
   
// Set the attribute 
$element->setAttribute('id', 'my_id'); 
   
// Append the child 
$dom->appendChild($element); 
    
// Get the elements 
$nodeList = $dom->getElementsByTagName('p'); 
foreach ($nodeList as $node) { 
    if($node->hasAttribute('id')) { 
        echo "Yes, id attribute is there."; 
    } 
} 
?>

輸出:

Yes, id attribute is there.

範例2:

<?php 
  
// Create a new DOMDocument 
$dom = new DOMDocument(); 
   
// Create a paragraph element 
$element = $dom->createElement('p',  
         'This is the paragraph element!'); 
  
// Set the attribute 
$element->setAttribute('class', 'my_class'); 
  
// Append the child 
$dom->appendChild($element); 
   
// Get the elements 
$nodeList = $dom->getElementsByTagName('p'); 
foreach ($nodeList as $node) { 
    if(!$node->hasAttribute('id')) { 
        echo "No, id attribute is not there."; 
    } 
} 
?>

輸出:

No, id attribute is not there.

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



相關用法


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