Tuesday, June 9, 2026
HomeLanguagesPHP | DOMNode removeChild() Function

PHP | DOMNode removeChild() Function

The DOMNode::removeChild() function is an inbuilt function in PHP which is used remove child from list of children.

Syntax:

DOMNode DOMNode::removeChild( DOMNode $oldnode )

Parameters: This function accepts a single parameter $oldnode which holds the child to remove.

Return Value: This function returns the removed child on success.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly and DOM_NOT_FOUND, if $oldnode is not a child of this node.

Below examples illustrate the DOMNode::removeChild() function in PHP:

Example 1:




<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a div element
$element = $document-> 
        appendChild(new DOMElement('div'));
  
// Create a text Node
$text1 = $document->
          createTextNode('neveropen');
  
// Append the nodes
$element->appendChild($text1);
  
// Remove the child
$element->removeChild($text1);
  
// Render the XML
echo $document->saveXML();
?>


Output: Press Ctrl+U to see the XML

Example 2:




<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a h1 element
$element = $document->
             appendChild(new DOMElement('h1'));
  
// Create the text Node
$text1 = $document->
               createTextNode('neveropen');
$text2 = $document->
          createTextNode('Text to be removed');
  
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
  
// Remove the child
$element->removeChild($text2);
  
// Render the output
echo $document->saveXML();
?>


Output:

Reference: https://www.php.net/manual/en/domnode.removechild.php

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6895 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS