PHP SimpleXML count() Function
In this chapter you will learn:
- Description for PHP count() Function
- Syntax for PHP count() Function
- Return for PHP count() Function
- 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:
- Description for PHP getDocNamespaces() Function
- Syntax for PHP getDocNamespaces() Function
- Parameter for PHP getDocNamespaces() Function
- Return for PHP getDocNamespaces() Function
- Example - Return the namespaces declared in the root of the XML document
- Example - Return all namespaces declared in parent and child nodes of the XML document
Home » PHP Tutorial » PHP SimpleXML Functions