DOMNode::compareDocumentPosition

(PHP 8 >= 8.4.0)

DOMNode::compareDocumentPositionCompares the position of two nodes

Description

public DOMNode::compareDocumentPosition(DOMNode $other): int

Compares the position of the other node relative to this node.

Parameters

other

The node for which the position should be compared for, relative to this node.

Return Values

A bitmask of the DOMNode::DOCUMENT_POSITION_* constants.

Examples

Example #1 DOMNode::compareDocumentPosition() example

<?php
$xml
= <<<XML
<root>
<child1/>
<child2/>
</root>
XML;

$dom = new DOMDocument();
$dom->loadXML($xml);

$root = $dom->documentElement;
$child1 = $root->firstElementChild;
$child2 = $child1->nextElementSibling;

var_dump($root->compareDocumentPosition($child1));
var_dump($child2->compareDocumentPosition($child1));
?>

The above example will output:

int(20) / This is DOMNode::DOCUMENT_POSITION_CONTAINED_BY | DOMNode::DOCUMENT_POSITION_FOLLOWING
int(2)  / This is DOMNode::DOCUMENT_POSITION_PRECEDING
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant