PHP | DOMElement hasAttribute() Function Last Updated : 05 Mar, 2020 Comments Improve Suggest changes Like Article Like Report The DOMElement::hasAttribute() function is an inbuilt function in PHP which is used to know whether attribute with a specific name exists as a member of the element. Syntax: bool DOMElement::hasAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name of attribute. Return Value: This function returns TRUE on success or FALSE on failure. Below given programs illustrate the DOMElement::hasAttribute() function in PHP: Program 1: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <div> <!-- id attribute is there --> <p id=\"prog\"> HELLO </p> </div> </root>"); // Get the elements $nodeList = $dom->getElementsByTagName('p'); foreach ($nodeList as $node) { if($node->hasAttribute('id')) { echo "Yes, id attribute is there."; } } ?> Output: Yes, id attribute is there. Program 2: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <div> <!-- id attribute is missing --> <p> HELLO </p> </div> </root>"); // Get the elements $nodeList = $dom->getElementsByTagName('p'); foreach ($nodeList as $node) { if(!$node->hasAttribute('id')) { echo "No, id attribute isn't there."; } } ?> Output: No, id attribute isn't there. Reference: https://www.php.net/manual/en/domelement.hasattribute.php Comment More infoAdvertise with us Next Article PHP | DOMElement hasAttribute() Function gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMElement hasAttributeNS() Function The DOMElement::hasAttributeNS() function is an inbuilt function in PHP which is used to know whether attribute in specific namespace named localName exists as a member of the element or not. Syntax: bool DOMElement::hasAttributeNS( string $namespaceURI, string $localName ) Parameters: This function 2 min read PHP | DOMElement getAttribute() Function The DOMElement::getAttribute() function is an inbuilt function in PHP which is used to get the value of the attribute with name for the current node. Syntax: string DOMElement::getAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the attribu 1 min read PHP | DOMElement getAttributeNS() Function The DOMElement::getAttributeNS() function is an inbuilt function in PHP which is used to get the value of the attribute in a specific namespace with local name for the current node. Syntax: string DOMElement::getAttributeNS( string $namespaceURI, string $localName ) Parameters: This function accepts 2 min read PHP | DOMElement getAttributeNode() Function The DOMElement::getAttributeNode() function is an inbuilt function in PHP which is used to get the attribute node with name, for the current element. Syntax: DOMAttr DOMElement::getAttributeNode( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the at 2 min read PHP | DOMElement setAttribute() Function The DOMElement::setAttribute() function is an inbuilt function in PHP which is used to set an attribute with given name to the given value. If the attribute does not exist, it will be created. Syntax: DOMAttr DOMElement::setAttribute( string $name, string $value ) Parameters: This function accepts t 2 min read Like