Friday, August 15, 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
32221 POSTS0 COMMENTS
Milvus
76 POSTS0 COMMENTS
Nango Kala
6594 POSTS0 COMMENTS
Nicole Veronica
11758 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11806 POSTS0 COMMENTS
Shaida Kate Naidoo
6701 POSTS0 COMMENTS
Ted Musemwa
6986 POSTS0 COMMENTS
Thapelo Manthata
6661 POSTS0 COMMENTS
Umr Jansen
6679 POSTS0 COMMENTS