PHP | DOMAttr isId() Function
Last Updated :
03 Sep, 2021
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
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$dom ->validateOnParse = true;
$element = $dom ->appendChild( new DOMElement( 'div' ));
$attr = $element ->setAttributeNode(
new DOMAttr( 'class' , 'geekforgeeks' ));
$getattr = $dom ->getElementsByTagName( 'div' )
->item(0)->getAttributeNode( 'class' );
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
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$dom ->validateOnParse = true;
$element = $dom ->appendChild( new DOMElement( 'div' ));
$attr = $element ->setAttributeNode(
new DOMAttr( 'id' , 'mynewid' ));
$element ->setIDAttribute( 'id' , true);
$getattr = $dom ->getElementsByTagName( 'div' )
->item(0)->getAttributeNode( 'id' );
if ( $getattr ->isId()) {
echo 'Yes, this is an id' ;
} else {
echo 'No, this is not an id' ;
}
?>
|
Output:
Yes, this is a id
Reference: https://www.php.net/manual/en/domattr.isid.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...