Saturday, September 6, 2025
HomeLanguagesPHP | xml_get_current_line_number() Function

PHP | xml_get_current_line_number() Function

The xml_get_current_line_number() function is an inbuilt function in PHP which is used to return the current line number for an XML parser.

Syntax: 

int xml_get_current_line_number( resource $xml_parser )

Parameters: This function accepts a single parameter $xml_parser which is required. It specifies the XML parser which is to be used.
Return Value: This function returns the current line number for the specified parser on which it’s currently running on success or False on failure.

Note:  

  • This function is available for PHP 4.0.0 and newer version.
  • These examples may not work on online IDE. So, try to run it on local server or php hosted servers.

gfg.xml file:  

XML




<?xml version="1.0" encoding="utf-8"?>
<user>
    <username> user123 </username>
    <name> firstname lastname </name>
    <phone> +91-9876543210 </phone>
    <detail> I am John Doe. Live in Kolkata, India. </detail>
</users>


PHP




<?php
 
// XML file containing mismatch tags
$xml_file = 'gfg.xml';
 
// XML parser initialization
$xml_parser = xml_parser_create();
 
// File opening in read mode
$file_pointer = fopen($xml_file, 'r');
  
// Reading data from the file stream
while ($xml_data = fread($file_pointer, 4096)) {
     
    // Parse the data chunk
    if(!xml_parse($xml_parser, $xml_data,
                        feof($file_pointer))) {
         
        // Displaying errors
        die( print "ERROR: " .
         
            // Error string
            xml_error_string(xml_get_error_code($xml_parser)) .
             
            "<br/>Error Code: " .
             
            // Error code
            xml_get_error_code($xml_parser) .
             
            "<br/>Line: " .
             
            // Line number where the error occurred
            xml_get_current_line_number($xml_parser) .
             
            "<br/>Column: " .
             
            // Column number where the error occurred
            xml_get_current_column_number($xml_parser) .
             
            "<br/>Byte Index: " .
             
            // Byte index where the current byte occurred
            xml_get_current_byte_index($xml_parser) . "<br/>"
        );
    }
}
 
// Free to XML parser
xml_parser_free($xml_parser);
 
?>


Output: 

ERROR: Mismatched tag
Error Code: 76
Line: 7
Column: 13
Byte Index: 208

neveropen.xml file:

XML




<?xml version="1.0 encoding="utf-8"?>
<user>
    <username> user123 </username>
    <name> firstname lastname </name>
    <phone> +91-9876543210 </phone>
    <detail> I am John Doe. Live in Kolkata, India. </detail>
</user>


Program 2: 

PHP




<?php
 
// XML file containing mismatch tags
$xml_file = 'neveropen.xml';
 
// XML parser initialization
$xml_parser = xml_parser_create();
 
// File opening in read mode
$file_pointer = fopen($xml_file, 'r');
  
// Reading data from the file stream
while ($xml_data = fread($file_pointer, 4096)) {
     
    // Parse the data chunk
    if(!xml_parse($xml_parser, $xml_data,
                        feof($file_pointer))) {
         
        // Displaying errors
        die( print "ERROR: " .
         
            // Error string
            xml_error_string(xml_get_error_code($xml_parser)) .
             
            "<br/>Error Code: " .
             
            // Error code
            xml_get_error_code($xml_parser) .
             
            "<br/>Line: " .
             
            // Line number where the error occurred
            xml_get_current_line_number($xml_parser) .
             
            "<br/>Column: " .
             
            // Column number where the error occurred
            xml_get_current_column_number($xml_parser) .
             
            "<br/>Byte Index: " .
             
            // Byte index where the current byte occurred
            xml_get_current_byte_index($xml_parser) . "<br/>"
        );
    }
}
 
// Free to XML parser
xml_parser_free($xml_parser);
 
?>


Output: 

ERROR: String not closed expecting " or '
Error Code: 34
Line: 1
Column: 36
Byte Index: 37

Reference: https://www.php.net/manual/en/function.xml-get-current-line-number.php
 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6641 POSTS0 COMMENTS
Nicole Veronica
11807 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6754 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS