PHP | DOMAttr isId() Function

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 accepts any parameters.



Return Value: This function returns TRUE if it containd an id attribute otherwise returns FALSE.

Below given programs illustrate the DOMAttr::isId() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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';
}
?>

chevron_right


Output:

No, this is not an id

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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';
}
?>

chevron_right


Output:

Yes, this is a id

Reference: https://www.php.net/manual/en/domattr.isid.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.