Friday, August 29, 2025
HomeLanguagesPHP | DOMNode isSameNode() Function

PHP | DOMNode isSameNode() Function

The DOMNode::isSameNode() function is an inbuilt function in PHP which indicates if two nodes are the same node or not.

Syntax:

bool DOMNode::isSameNode( DOMNode $node )

Parameters: This function accepts a single parameter $node which holds the node to be compared.

Return Value: This function returns TRUE on success or FALSE on failure.

Below given programs illustrate the DOMNode::isSameNode() function in PHP:

Program 1:




<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
  
// Create a paragraph element with a namespace
$p_element = $dom->createElementNS(
      'my_namespace', 'p', 'neveropen');
  
// Append the child to DOMDocument
$dom->appendChild($p_element);
  
// Check if the node is same
$isSameNode = $dom->isSameNode($dom);
  
// Check if the namespace is default or not
if($isSameNode) {
    echo 'Yes, $dom is same to itself.';
}
?>


Output:

Yes, $dom is same to itself.

Program 2:




<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
  
// Create a paragraph element with a namespace
$p_element = $dom->createElementNS(
      'my_namespace', 'p', 'neveropen');
  
// Append the child to DOMDocument
$dom->appendChild($p_element);
  
// Create another new DOMDocument instance
$dom2 = new DOMDocument();
  
// Check if nodes are same
$isSameNode = $dom->isSameNode($dom2);
  
// Check if the namespace is default or not
if(!$isSameNode) {
    echo 'No, $dom and $dom2 are different.';
}
?>


Output:

No, $dom and $dom2 are different.

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

RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11791 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS