PHP | SimpleXMLElement attributes() Function Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Pre-requisite: Read XML BasicsThe SimpleXMLElement::attributes() function is an inbuilt function in PHP which is used to retrieve the attributes and its value from an XML tag in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::attributes( $namespace, $is_prefix ) Parameter: This function accepts two parameters as mentioned above and described below: $namespace: It is optional parameter. It specifies the namespace for retrieved Attribute.$is_prefix: It is boolean parameter. It is True if $namespace is a prefix and False if $namespace is URI. Its default value is False. Return Value: It returns a SimpleXMLElement object which can be iterated over attributes of a tag of the SimpleXMLElement object. It returns null if the SimpleXMLElement Object is already an attribute not a tag.Note: This function is available on PHP 5.0.1 and newer version.Below programs illustrate the SimpleXMLElement::attributes() function in PHP:Program 1: php <?php // Loading XML document to $user $user = <<<XML <user> <username> Geeks123 </username> <name> GeeksforGeeks </name> <phone> +91-XXXXXXXXXX </phone> <address font-color="blue" font="awesome-fonts" font-size="24px"> Noida, UP, India </address> </user> XML; // Loading string as simple xml object $xml = simplexml_load_string($user); // Print children attribute with its value foreach($xml->address[0]->attributes() as $key => $value) { echo $key . " => " . $value . "</br>"; } ?> Output: font-color => blue font => awesome-fonts font-size => 24px Program 2: php <?php // Loading XML document to $user $user = <<<XML <user> <username font-color="green" font="awesome-fonts" font-size="72px"> Geeks123 </username> <name font-color="blue" font="awesome-fonts" font-size="36px"> GeeksforGeeks </name> <phone font-color="blue" type="number" font="awesome-fonts" font-size="24px"> +91-XXXXXXXXXX </phone> <address font-color="blue" font="awesome-fonts" font-size="24px"> Noida, UP, India </address> </user> XML; // Loading string as simple xml object $xml = simplexml_load_string($user); // Print children attribute foreach($xml->children() as $child) { echo "Child name: " . $child->getName() . " =>" . $child . "<br>"; foreach($child->attributes() as $key => $value) echo " parameter: " . $key . " => " . $value . "</br>"; } ?> Output: Child name: username => Geeks123 parameter: font-color => green parameter: font => awesome-fonts parameter: font-size => 72px Child name: name => GeeksforGeeks parameter: font-color => blue parameter: font => awesome-fonts parameter: font-size => 36px Child name: phone => +91-XXXXXXXXXX parameter: font-color => blue parameter: type => number parameter: font => awesome-fonts parameter: font-size => 24px Child name: address => Noida, UP, India parameter: font-color => blue parameter: font => awesome-fonts parameter: font-size => 24px Reference: https://www.php.net/manual/en/simplexmlelement.attributes.php Comment More infoAdvertise with us Next Article PHP | SimpleXMLElement count() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | SimpleXMLElement addAttribute() Function Pre-requisite: Read XML Basics The SimpleXMLElement::addAttribute() function is an inbuilt function in PHP which add an attribute in a SimpleXML object. Syntax: void SimpleXMLElement::addAttribute($name, $value, $namespace) Parameter: This function accepts three parameters as mentioned above and des 2 min read PHP | SimpleXMLElement::__construct() Function Pre-requisite:XML The __construct() function is an inbuilt function in PHP that is used to create a new SimpleXMLElement object for XML. Syntax: SimpleXMLElement::__construct( $data, $options, $data_is_url, $namespace, $is_prefix ) Parameters: This function accepts five parameters as mentioned abov 3 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 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 children() Function Pre-requisite: Read XML BasicsThe SimpleXMLElement::children() function is an inbuilt function in PHP which returns children of a given node in a SimpleXML object. Syntax:  SimpleXMLElement SimpleXMLElement::children( $namespace, $is_prefix ) Parameter: This function accepts two parameters as ment 3 min read PHP | SimpleXMLElement addChild() Function Pre-requisite: Read XML Basics The SimpleXMLElement::addChild() function is an inbuilt function in PHP which is used to add a child in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::addChild($name, $value, $namespace); Parameter: This function accepts three parameters as mentioned ab 1 min read Like