大网数据广告活动
PHP 语言与运行手册

DOMNode::compareDocumentPosition · Compares the position of two nodes

· 52CE 我爱测

DOMNode::compareDocumentPosition — Compares the position of two nodes

DOMNode::compareDocumentPosition

(PHP 8 >= 8.4.0)

DOMNode::compareDocumentPosition — Compares the position of two nodes

Description

public function 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

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。