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

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// 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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions