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

Description

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

Syntax

PHP count() Function has the following syntax.

count();

Return

Returns the number of children of an element

Example

Count the children of the book nodes:


<?php//from   j  a  v a  2 s .  c o  m
$xml=<<<XML
  <books>
    <book name="PHP">
    <child/>
  </book>
  <book name="Java">
    <child/>
    <child/>
  </book>
</books>
XML;

$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