PHP xml_get_current_line_number() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
XML Parser

Definition

In PHP, xml_get_current_line_number() returns the line number the XML parser is currently reading.

It is the quickest way to tell a user where a problem is in an XML file. Line numbers start at 1, and the function works inside handlers or right after xml_parse() fails.

Syntax

Call xml_get_current_line_number() with the values you need. Start with this line.

PHP
$line = xml_get_current_line_number($parser);
CallWhat it does
xml_get_current_line_number($parser)Returns the current line number, starting at 1.
xml_get_current_line_number($parser) . ":" . xml_get_current_column_number($parser)Builds a line:column position for messages.
"Error on line " . xml_get_current_line_number($parser)A common message after xml_parse() returns 0.

Line numbers are counted from 1. After a failed xml_parse() the number tells you which line contains the problem.

Library declarations
PHP
xml_get_current_line_number(XMLParser $parser): int

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().

Return Value

Return ValueDescription
intThe current line number, where the first line is 1.

PHP Examples

Example 1 — Line of Each Start Tag

Parse a small multi-line XML string and print the line number of every start tag.

PHP
<?php
$xml = "<root>\n  <item>Tea</item>\n  <item>Milk</item>\n</root>";

$parser = xml_parser_create();
xml_set_element_handler(
    $parser,
    function ($parser, $name) {
        echo $name, " at line ", xml_get_current_line_number($parser), PHP_EOL;
    },
    function ($parser, $name) {
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. The XML string uses \n to put each tag on its own line.
  2. The start handler runs once for every opening tag.
  3. xml_get_current_line_number() reports the line of the tag being read.
  4. ROOT is on line 1, and the two ITEM tags are on lines 2 and 3.

Example 2 — Report the Line of an Error

When xml_parse() fails, read the line number to tell the user where the mistake is.

PHP
<?php
$xml = "<root>\n<item>Tea</root>";

$parser = xml_parser_create();
$result = xml_parse($parser, $xml, true);

echo "Parse result: ", $result, PHP_EOL;
echo "Error on line: ", xml_get_current_line_number($parser), PHP_EOL;

How It Works

  1. The item tag on line 2 is closed by </root>, which does not match.
  2. xml_parse() stops at the problem and returns 0.
  3. xml_get_current_line_number() still works after the failure.
  4. It reports line 2, where the mismatched closing tag was found.

Example 3 — Line Numbers for Text Content

The character data handler also runs at a specific line. Skip the blank whitespace and print where each piece of text lives.

PHP
<?php
$xml = "<root>\n  <item>Tea</item>\n  <item>Milk</item>\n</root>";

$parser = xml_parser_create();
xml_set_character_data_handler(
    $parser,
    function ($parser, $data) {
        $text = trim($data);
        if ($text !== '') {
            echo $text, " is on line ", xml_get_current_line_number($parser), PHP_EOL;
        }
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. xml_set_character_data_handler() registers a function for text between tags.
  2. The line breaks and spaces also arrive as text, so trim() removes them.
  3. Empty results are skipped with the if check.
  4. Tea and Milk print with the lines where they appear.

Frequently Asked Questions

It returns the line number where the parser currently is. The first line of the document is line 1.
Yes. It is one of the best ways to find the line of a syntax problem. Call it right after xml_parse() returns 0.
It starts at 1 for the line number. The column number and byte index start at 0.
Yes. Call it inside an element or character data handler to log the line of each tag or piece of text.
Combine it with xml_get_current_column_number(), for example "line 2, column 2". Add 1 to the column if you want it to match editors.

Next: xml_get_error_code()

xml_get_error_code() returns the number of the last error that happened on an XML parser.

xml_get_error_code() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful