Skip to content
Related Articles

Related Articles

PHP | DOMNode getLineNo() function
  • Last Updated : 25 Feb, 2020

The DOMNode::getLineNo() function is an inbuilt function in PHP which is used to get the line number for where the node is defined.

Syntax:

DOMNode DOMNode::getLineNo( void )

Parameters:This function doesn’t accept any parameter.

Return Value: This function returns the line number where the node was defined in.

Below given programs illustrate the DOMNode::getLineNo() function in PHP:
Program 1:






<?php
// Create a XML variable
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root>
    <h1>GeeksforGeeks</h1>
</root>
XML;
  
// Create a new DOMDocument instance
$dom = new DOMDocument;
  
// Load the XML
$dom->loadXML($xml);
  
// Print where the line where the 'node' element was defined in
echo 'The <node> tag is defined on line ' . $dom->getElementsByTagName('h1')->item(0)->getLineNo();
?>

Output:

The tag is defined on line 3

Program 2:




<?php
// Create a XML variable
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root>
    <h1>Geeks</h1>
    <h1>For</h1>
    <h1>Geeks</h1>
</root>
XML;
  
// Create a new DOMDocument instance
$dom = new DOMDocument();
  
// Load the XML
$dom->loadXML($xml);
  
for ($i = 0; $i < 3; $i++) {
    // Print where the line where the 'node' element was defined in
    echo $i . ') The h1 tag is defined on line ' . $dom->getElementsByTagName('h1')->item($i)->getLineNo() . "<br>";
  
}
?>

Output:

0) The h1 tag is defined on line 3
1) The h1 tag is defined on line 4
2) The h1 tag is defined on line 5

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :