array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'domnode.comparedocumentposition.php', 1 => 'DOMNode::compareDocumentPosition', 2 => 'Compares the position of two nodes', ), 'up' => array ( 0 => 'class.domnode.php', 1 => 'DOMNode', ), 'prev' => array ( 0 => 'domnode.clonenode.php', 1 => 'DOMNode::cloneNode', ), 'next' => array ( 0 => 'domnode.contains.php', 1 => 'DOMNode::contains', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/dom/domnode/compareDocumentPosition.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

DOMNode::compareDocumentPosition

(PHP 8 >= 8.4.0)

DOMNode::compareDocumentPositionCompares the position of two nodes

说明

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

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

参数

other

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

返回值

A bitmask of the DOMNode::DOCUMENT_POSITION_* constants.

示例

示例 #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));
?>

以上示例会输出:

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