PHP | DOMElement getAttribute() Function Last Updated : 20 Feb, 2020 Comments Improve Suggest changes Like Article Like Report 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 attribute. Return Value: This function returns an string value containing the attribute value. Below given programs illustrate the DOMElement::getAttribute() function in PHP: Program 1: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <body> <strong attr=\"value\"> 22 </strong> </body>"); // Get the strong element $element = $dom->getElementsByTagName('strong'); // Get the attribute $value = $element[0]->getAttribute('attr'); echo $value; ?> Output: value Program 2: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <body> <div id=\"div1\"> DIV 1 </div> <div id=\"div2\"> DIV 2 </div> <div id=\"div3\"> DIV 3 </div> </body>"); // Get all the div elements $elements = $dom->getElementsByTagName('div'); // Get the id value of each element echo "All the id values of divs are: <br>"; foreach ($elements as $element) { echo $element->getAttribute('id') . "<br>"; } ?> Output: All the id values of divs are: div1 div2 div3 Reference: https://www.php.net/manual/en/domelement.getattribute.php Comment More infoAdvertise with us Next Article PHP | DOMElement getAttribute() Function gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads 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 getAttributeNodeNS() Function The DOMElement::getAttributeNodeNS() function is an inbuilt function in PHP which is used to get the attribute node in specific namespace with local name for the current node. Syntax: DOMAttr DOMElement::getAttributeNodeNS( string $namespaceURI, string $localName ) Parameters: This function accepts 2 min read PHP | DOMElement hasAttribute() Function 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 1 min read 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 Like