PHP | SimpleXMLElement::getName() Function Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Pre-requisite: Read XML basicsThe SimpleXMLElement::getName() function is an inbuilt function in PHP which returns the name of the xml element.Syntax: string SimpleXMLElement::getName( void ) Parameter: This function does not accept any parameter.Return Value: It returns a string which represents the name of XML element of a SimpleXMLElement object.Note: This function is available on PHP 5.1.3 and newer version.Below programs illustrate the SimpleXMLElement::getName() function in PHP:Example 1: php <?php // Loading XML document to $user $user = <<<XML <user> <username>Geeks123 </username> <name>GeeksforGeeks</name> <phone>+91-XXXXXXXXXX</phone> <detail font-color="blue" font-size="24px"> Noide India </detail> </user> XML; // Loading string as simple xml object $xml = simplexml_load_string($user); // Display the name of element echo "Base tag name: " . $xml->getName() . "<br>"; foreach($xml->children() as $child) { echo "child node: " . $child->getName() . " = " . $child . "</br>"; } ?> Output: Example 2: php <?php // Loading XML document to $user $user = <<<XML <user> <username>Geeks123</username> <name>GeeksforGeeks</name> <phone>+91-XXXXXXXXXX</phone> <detail font-color="blue" font-size="24px"> Computer science portal </detail> <address> <city>Noida</city> <country>India</country> </address> </user> XML; // Loading string as simple xml object $xml = simplexml_load_string($user); // Recursive function called getname_rec($xml, 0); // The getname_rec() function definition function getname_rec($xml, $depth) { print_space($depth); echo "tag name: " . $xml->getName() . "<br>"; foreach($xml->children() as $child) { if($child->count() > 0) { // If there exists any child of current node getname_rec($child, $depth+1); } else { // If there is no child of the current node print_space($depth); echo " child node: " . $child->getName() . " = " . $child . "</br>"; } } } // Function to print 3X$i number of spaces function print_space($i) { for($x = 0; $x < $i*3; $x++) { echo " "; } } ?> Output: Reference:https://www.php.net/manual/en/simplexmlelement.getname.php Comment More infoAdvertise with us Next Article PHP | SimpleXMLElement asXML() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | SimpleXMLElement getNamespaces() Function Pre-requisite: Read XML basics The SimpleXMLElement::getNamespaces() function is an inbuilt function in PHP which is used to retrieve the namespaces declared in XML document. Syntax: array SimpleXMLElement::getNamespaces( $recursive ) Parameter: This function accepts single parameter $recursive whic 2 min read PHP | SimpleXMLElement getDocNamespaces() Function Pre-requisite: Read XML Basics The SimpleXMLElement::getDocNamespaces() function is an inbuilt function in PHP which is used to retrieve the namespaces declared in XML document. Syntax: array SimpleXMLElement::getDocNamespaces( $recursive, $from_root ) Parameters: This function accepts two parameter 2 min read PHP | SimpleXMLElement asXML() Function Pre-requisite: Read XML The SimpleXMLElement::asXML() function is an inbuilt function in PHP which returns well-formed XML string from a SimpleXML object. Syntax: mixed SimpleXMLElement::asXML( $filename ) Parameters: This function accepts single parameter $filename which is optional. It specified t 2 min read PHP | SimpleXMLElement XPath() Function Pre-requisite: Read XML Basics The SimpleXMLElement::xpath() function is an inbuilt function in PHP which runs XPath query on the XML document. Syntax: SimpleXMLElement::xpath( $path ) Parameters: This function accepts single parameter $path which is required. It is used to specify the XPath path of 2 min read PHP | SimpleXMLElement count() Function Pre-requisite: Read XML Basics The SimpleXMLElement::count() function is an inbuilt function in PHP which counts number of child element in a SimpleXML object. Syntax: int SimpleXMLElement::count() Parameter: This function does not accept any parameters. Return Value: This function returns number of 2 min read PHP | SimpleXMLElement saveXML() Function The SimpleXMLElement::saveXML() function is an inbuilt function in PHP which return well-formed XML string from a SimpleXML object. It is an alias of SimpleXMLElement::asXML() function. Syntax: mixed SimpleXMLElement::saveXML( string $filename ) Parameter: This function accepts single parameter $fil 2 min read Like