PHP | SimpleXMLElement count() Function Last Updated : 29 May, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 children of an element. Note: This function is available for PHP 5.3.0 and newer version. Example 1: php <?php // Loading XML document to $user $user = <<<XML <user> <username> user123 </username> <name> firstname lastname </name> <phone> +91-9876543210 </phone> <detail> I am John Doe. Live in Kolkata, India. </detail> </user> XML; // Creating new SimpleXMLElement // object from $user $xml = new SimpleXMLElement($user); // Counting and printing number of // child of the XML document echo $xml->count(); ?> Output: 4 Example 2: Counting children of child element of the XML document. php <?php // Loading XML document to $user $user = <<<XML <users> <user name="user1"> <username> user123 </username> <name> firstname lastname </name> <phone> +91-9876543210 </phone> <detail> I am John Doe. Live in Kolkata, India. </detail> <ins> <ins_name>geeks for geeks</ins_name> <ins_type>educational</ins_type> <ins_url>geeksforgeeks.org</ins_url> </ins> </user> <user name="user2"> <username> user123 </username> <name> firstname lastname </name> <phone> +91-9876543210 </phone> <detail> I am John Doe. Live in Kolkata, India. </detail> <ins> <ins_name>geeks for geeks</ins_name> <ins_type>educational</ins_type> <ins_url>geeksforgeeks.org</ins_url> </ins> </user> </users> XML; // Creating new SimpleXMLElement // object from $user $xml = new SimpleXMLElement($user); echo $xml->count(); foreach($xml as $child){ echo "<br>".$child['name'] . " has " . $child->count()." child."; } ?> Output: 2 user1 has 5 child. user2 has 5 child. Reference: https://www.php.net/manual/en/simplexmlelement.count.php Comment More infoAdvertise with us Next Article PHP | SimpleXMLElement attributes() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads 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 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 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 attributes() Function 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 fun 2 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 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 Like