As the name indicates SimpleXMLElement::children is a member function is simply returns the children elements as objects from SimpleXMLElement class object.
It always return a 'SimpleXMLElement' class object unless the node represents an attribute (id, class etc can be consider as attributes), in which case null is returned.
Consider this example
$foo = new SimpleXmlElement(
'<foo>
<bar>
<baz id="b1">b1</baz>
<baz id="b2">b2</baz>
<baz id="b3">b3</baz>
<pas id="p1">p1</pas>
</bar>
</foo>');
$mainfoo = json_encode($foo); var_dump($mainfoo); // string(50) "{"bar":{"baz":["b1","b2","b3"],"pas":["p1","p2"]}}"
$children = json_encode($foo->children()); var_dump($children ); // string(50) "{"bar":{"baz":["b1","b2","b3"],"pas":["p1","p2"]}}"
$childrenofbar = json_encode($foo->bar->children()); var_dump($childrenofbar ); //string(42) "{"baz":["b1","b2","b3"],"pas":["p1","p2"]}"
$children_bar_pas = json_encode($foo->bar->pas); var_dump($children_bar_pas); //string(45) "{"@attributes":{"id":"p1"},"0":"p1","1":"p2"}"
We can not use var_dump to view and verify SimpleXmlElement object. So for convenient manner , we can simply json_encode them and var_dump them ;)
Here the first statement converts an xml/markup string to SimpleXmlElement class object.
$foo->children() brings the children elements as a SimpleXmlElement class objects
$foo->bar->children() brings the children elements of 'bar'.
As per the documentation, object of a SimpleXmlElement is iterable using php loops such as for/ foreach etc..
foreach($foo as $f1) {
var_dump(json_encode($f1)); // "{"baz":["b1","b2","b3"],"pas":["p1","p2"]}"
}
foreach($foo->children() as $f2) {
var_dump(json_encode($f2)); // string(42) "{"baz":["b1","b2","b3"],"pas":["p1","p2"]}"
}
children() method can also use to access child elements for the markups having valid namespace. Please see the Example #2 in documentation!