php hasattribute,PHP DOMElement hasAttribute()用法及代码示例
DOMElement::hasAttribute()函數是PHP中的內置函數,用于了解具有特定名稱的屬性是否作為元素的成員存在。
用法:
bool DOMElement::hasAttribute( string $name )
參數:該函數接受單個參數$name,該參數保存屬性的名稱。
返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。
下面給出的程序說明了PHP中的DOMElement::hasAttribute()函數:
程序1:
// Create a new DOMDocument
$dom = new DOMDocument();
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
HELLO
");
// 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:
// Create a new DOMDocument
$dom = new DOMDocument();
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
HELLO
");
// Get the elements
$nodeList = $dom->getElementsByTagName('p');
foreach ($nodeList as $node) {
if(!$node->hasAttribute('id')) {
echo "No, id attribute isn't there.";
}
}
?>
輸出:
No, id attribute isn't there.
總結
以上是生活随笔為你收集整理的php hasattribute,PHP DOMElement hasAttribute()用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 插入 基准测试,oracl
- 下一篇: graphpad做折线图后怎么保存_农村