W3docs

xml_get_current_line_number()

The xml_get_current_line_number() function is a PHP built-in function that retrieves the current line number of an XML parser. When parsing an XML file using

The xml_get_current_line_number() function is a PHP built-in function that retrieves the current line number of an XML parser. It belongs to the procedural XML Parser (Expat) extension and is used when parsing XML with the SAX-style xml_parse* functions. Knowing the current line number is useful for debugging, error reporting, and tracking parsing progress.

This page covers the function's syntax, its return value, and practical examples — both with a one-shot parse and inside a live SAX event handler.

When to use it

Call xml_get_current_line_number() whenever you need the parser's position in the source document, typically:

This function is part of the procedural Expat API. If you parse XML with the object-oriented XMLReader class instead, use its getLineNo() method.

Syntax

The syntax of the xml_get_current_line_number() function is as follows:

xml_get_current_line_number($parser): int|false

Parameter

  • $parser — the XML parser created by xml_parser_create() (or xml_parser_create_ns()). In PHP 8+ this is an XMLParser object; in PHP 7 and earlier it is a resource.

Return value

Returns the current line number (an int, starting at line 1) as the parser reads the document. It returns false if $parser is not a valid parser. During parsing, the value reflects the line of the data the parser has reached — so it is most meaningful when read inside an event handler.

Usage Examples

Let's take a look at some practical examples of using xml_get_current_line_number() in PHP.

Example 1: Retrieving the Current Line Number of an XML Parser

Suppose you have an XML file data.xml that you want to parse using the XML Parser extension in PHP. You can use the xml_get_current_line_number() function to retrieve the line number reached after parsing, like this:

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($xml_parser, file_get_contents("data.xml"), $values);
$current_line_number = xml_get_current_line_number($xml_parser);
echo "Current Line Number: $current_line_number";
xml_parser_free($xml_parser);

This code creates an XML parser using xml_parser_create(), and sets an option to turn off case folding. It then uses xml_parse_into_struct() to parse the XML file data.xml and store the result in an array $values. It retrieves the current line number using xml_get_current_line_number(), which returns the line number of the last element parsed, and prints it to the console. Finally, it frees the memory used by the XML parser using xml_parser_free().

Example 2: Tracking Line Numbers During SAX Parsing

Suppose you want to track the line number of each element as it is parsed using a SAX event handler. You can use xml_set_element_handler() along with xml_get_current_line_number() to monitor the parser's position in real time, like this:

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, function($parser, $name, $attrs) {
    $line = xml_get_current_line_number($parser);
    echo "Element <$name> found at line $line\n";
}, function($parser, $name) {
    // end element handler
});
$xml_data = file_get_contents("data.xml");
xml_parse($xml_parser, $xml_data, true);
xml_parser_free($xml_parser);

This code creates an XML parser using xml_parser_create(), and registers an element handler using xml_set_element_handler(). It then uses xml_parse() to parse the XML file data.xml. Each time a start tag is encountered, the handler retrieves the current line number with xml_get_current_line_number() and outputs the element name along with its line position. Finally, it frees the parser with xml_parser_free(). Because the line number is read during the callback, it reflects the real position of each element — unlike Example 1, where the value reflects only the end state after the whole document was parsed.

Common gotcha

xml_get_current_line_number() is meaningful only while parsing is in progress, which in practice means inside an event handler. If you call it after xml_parse() has finished (as in Example 1), you get the line of the last byte consumed, not the line of any specific element. For per-element positions, always read it inside the handler as shown in Example 2.

Conclusion

In this article, we've discussed PHP's xml_get_current_line_number() function and how it retrieves the current line number of an XML parser. We covered what the function does, its syntax and return value, and showed it in both a one-shot parse and a live SAX handler. By reading the line number inside your event handlers, you can pinpoint elements in the source document for debugging, error reporting, and progress tracking.

Practice

Practice
In the procedural XML Parser (Expat) extension, how do you get the line the parser is currently on?
In the procedural XML Parser (Expat) extension, how do you get the line the parser is currently on?
Was this page helpful?