PHP addChild() Function
Description
The addChild() function adds a child element to the SimpleXML element.
Syntax
PHP addChild() Function has the following syntax.
addChild(name,value,ns);
Parameter
Parameter | Is Required | Description |
---|---|---|
name | Required. | Name of the child element to add |
value | Optional. | Value of the child element |
ns | Optional. | A namespace for the child element |
Return
Returns a SimpleXMLElement object that represents the child added to the XML.
Example
Add a child element to the body element and a footer element:
<?php// w ww.j ava2 s . com
$note=<<<XML
<book>
<name>PHP</name>
<name>Java</name>
</book>
XML;
$xml=new SimpleXMLElement($note);
$xml->body->addChild("date","2013-01-01");
$footer=$xml->addChild("Chapter","Some Chapter");
echo $xml->asXML();
?>
The code above generates the following result.