PHP Tutorial - PHP addChild() Function






The addChild() function adds a child element to the SimpleXML element.

Syntax

PHP addChild() Function has the following syntax.

addChild(name,value,ns);

Parameter

ParameterIs RequiredDescription
nameRequired.Name of the child element to add
valueOptional.Value of the child element
nsOptional.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//from w w w .j av  a2s  .c om
$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.