<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Create new element node</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="domdocument.createdocumentfragment.html">DOMDocument::createDocumentFragment</a></div> <div class="next" style="text-align: right; float: right;"><a href="domdocument.createelementns.html">DOMDocument::createElementNS</a></div> <div class="up"><a href="class.domdocument.html">DOMDocument</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="domdocument.createelement" class="refentry"> <div class="refnamediv"> <h1 class="refname">DOMDocument::createElement</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">DOMDocument::createElement</span> — <span class="dc-title">Create new element node</span></p> </div> <div class="refsect1 description" id="refsect1-domdocument.createelement-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.domelement.html" class="type DOMElement">DOMElement</a></span> <span class="methodname"><strong>DOMDocument::createElement</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$value</code></span> ] )</div> <p class="para rdfs-comment"> This function creates a new instance of class <a href="class.domelement.html" class="classname">DOMElement</a>. This node will not show up in the document unless it is inserted with (e.g.) <span class="function"><a href="domnode.appendchild.html" class="function">DOMNode::appendChild()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-domdocument.createelement-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">name</code></em></span> <dd> <p class="para"> The tag name of the element. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">value</code></em></span> <dd> <p class="para"> The value of the element. By default, an empty element will be created. The value can also be set later with <a href="class.domnode.html#domnode.props.nodevalue" class="link">DOMElement::$nodeValue</a>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-domdocument.createelement-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a new instance of class <a href="class.domelement.html" class="classname">DOMElement</a> or <strong><code>FALSE</code></strong> if an error occurred. </p> </div> <div class="refsect1 errors" id="refsect1-domdocument.createelement-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> <dl> <dt> <span class="term"><strong><code>DOM_INVALID_CHARACTER_ERR</code></strong></span> <dd> <p class="para"> Raised if <em><code class="parameter">name</code></em> contains an invalid character. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 examples" id="refsect1-domdocument.createelement-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5399"> <p><strong>Example #1 Creating a new element and inserting it as root</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$dom </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">(</span><span style="color: #DD0000">'1.0'</span><span style="color: #007700">, </span><span style="color: #DD0000">'utf-8'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$element </span><span style="color: #007700">= </span><span style="color: #0000BB">$dom</span><span style="color: #007700">-></span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">, </span><span style="color: #DD0000">'This is the root element!'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// We insert the new element as root (child of the document)<br /></span><span style="color: #0000BB">$dom</span><span style="color: #007700">-></span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$dom</span><span style="color: #007700">-></span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> <?xml version="1.0" encoding="utf-8"?> <test>This is the root element!</test> </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-domdocument.createelement-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <em><code class="parameter">value</code></em> will <em class="emphasis">not</em> be escaped. Use <span class="methodname"><a href="domdocument.createtextnode.html" class="methodname">DOMDocument::createTextNode()</a></span> to create a text node with <em class="emphasis">escaping support</em>. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-domdocument.createelement-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="domnode.appendchild.html" class="methodname" rel="rdfs-seeAlso">DOMNode::appendChild()</a> - Adds new child at the end of the children</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createattribute.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createAttribute()</a> - Create new attribute</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createattributens.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createAttributeNS()</a> - Create new attribute node with an associated namespace</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createcdatasection.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createCDATASection()</a> - Create new cdata node</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createcomment.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createComment()</a> - Create new comment node</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createdocumentfragment.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createDocumentFragment()</a> - Create new document fragment</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createelementns.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createElementNS()</a> - Create new element node with an associated namespace</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createentityreference.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createEntityReference()</a> - Create new entity reference node</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createprocessinginstruction.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createProcessingInstruction()</a> - Creates new PI node</span></li> <li class="member"> <span class="methodname"><a href="domdocument.createtextnode.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createTextNode()</a> - Create new text node</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="domdocument.createdocumentfragment.html">DOMDocument::createDocumentFragment</a></div> <div class="next" style="text-align: right; float: right;"><a href="domdocument.createelementns.html">DOMDocument::createElementNS</a></div> <div class="up"><a href="class.domdocument.html">DOMDocument</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>