PHP SimpleXML count() Function

In this chapter you will learn:

  1. Description for PHP count() Function
  2. Syntax for PHP count() Function
  3. Return for PHP count() Function
  4. Example - Count the children of the book nodes


The count() function counts the children of a specified node.


PHP count() Function has the following syntax.



Returns the number of children of an element


Count the children of the book nodes:

<?php//from   j  a  v a  2 s .  c o  m
    <book name="PHP">
  <book name="Java">

$elem=new SimpleXMLElement($xml);
foreach ($elem as $book){
   printf("%s has %d children.\n", $book['name'], $book->count());

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP getDocNamespaces() Function
  2. Syntax for PHP getDocNamespaces() Function
  3. Parameter for PHP getDocNamespaces() Function
  4. Return for PHP getDocNamespaces() Function
  5. Example - Return the namespaces declared in the root of the XML document
  6. Example - Return all namespaces declared in parent and child nodes of the XML document
Home » PHP Tutorial » PHP SimpleXML Functions
PHP SimpleXMLElement Create
PHP addAttribute() Function
PHP addChild() Function
PHP asXML() Function
PHP attributes() Function
PHP children() Function
PHP SimpleXML count() Function
PHP getDocNamespaces() Function
PHP getName() Function
PHP getNamespaces() Function
PHP registerXPathNamespace() Function
PHP simplexml_import_dom() Function
PHP simplexml_load_file() Function
PHP simplexml_load_string() Function
PHP xpath() Function