The DOMAttr::isId() function is an inbuilt function in PHP which is used to check if an attribute is a defined ID or not. According to the DOM standard, it requires the attribute ID to be of type ID. You need to validate your document with DOMDocument::validateOnParse() method before using this function.
Syntax:
bool DOMAttr::isId( void )
Parameters: This function doesn’t accept any parameters.
Return Value: This function returns TRUE if it contained an id attribute otherwise returns FALSE.
Below given programs illustrate the DOMAttr::isId() function in PHP:
Program 1:
<?php
// Create a new DOM Document
$dom = new DOMDocument('1.0', 'iso-8859-1');
// Enable validate on parse
$dom->validateOnParse = true;
// Create a div element
$element = $dom->appendChild(new DOMElement('div'));
// Create a class attribute
$attr = $element->setAttributeNode(
new DOMAttr('class', 'geekforgeeks'));
// Get the attribute
$getattr = $dom->getElementsByTagName('div')
->item(0)->getAttributeNode('class');
// Check if it is id or not
if($getattr->isId()) {
echo 'Yes, this is an id';
} else {
echo 'No, this is not an id';
}
?>
Output:
No, this is not an id
Program 2:
<?php
// Create a new DOM Document
$dom = new DOMDocument('1.0', 'iso-8859-1');
// Enable validate on parse
$dom->validateOnParse = true;
// Create a div element
$element = $dom->appendChild(new DOMElement('div'));
// Create a id attribute
$attr = $element->setAttributeNode(
new DOMAttr('id', 'mynewid'));
// Set that attribute as id
$element->setIDAttribute('id', true);
// Get the attribute
$getattr = $dom->getElementsByTagName('div')
->item(0)->getAttributeNode('id');
// Check if it is id or not
if($getattr->isId()) {
echo 'Yes, this is an id';
} else {
echo 'No, this is not an id';
}
?>
Output:
Yes, this is a id