PHP xml_get_current_column_number() Function

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

Definition

In PHP, xml_get_current_column_number() returns the column where the XML parser currently is on the current line.

Together with xml_get_current_line_number() it gives a position people can understand, like line 2, column 2. The column count starts at 0 and resets at the start of every new line.

Syntax

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

PHP
$column = xml_get_current_column_number($parser);
CallWhat it does
xml_get_current_column_number($parser)Returns the zero-based column of the current parser event.
xml_get_current_line_number($parser)Pair it with the line number to build a full position message.
xml_get_current_column_number($parser) + 1Gives a 1-based column if you want to show it to a person.

The column starts at 0, and it resets after each line break. Call it inside a handler or right after xml_parse() fails.

Library declarations
PHP
xml_get_current_column_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 zero-based column number on the current line.

PHP Examples

Example 1 — Column of Each Start Tag

Print the column of every start tag in a one-line XML string.

PHP
<?php
$xml = '<root><item id="1">Tea</item></root>';

$parser = xml_parser_create();
xml_set_element_handler(
    $parser,
    function ($parser, $name) {
        echo $name, " starts at column ", xml_get_current_column_number($parser), PHP_EOL;
    },
    function ($parser, $name) {
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. The XML is on a single line, so the column is the same as the character position.
  2. ROOT begins at the very start, which is column 0.
  3. <root> is 6 characters long, so ITEM begins at column 6.
  4. Names are upper case because the parser folds case by default.

Example 2 — Columns Reset on Each Line

When the XML has line breaks, the column starts again at 0 on every new line. Print the line and the column together.

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

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

xml_parse($parser, $xml, true);

How It Works

  1. The double-quoted PHP string contains real line breaks written as \n.
  2. ROOT is on line 1 at column 0.
  3. ITEM is on line 2 after two spaces of indentation, so its column is 2.
  4. xml_get_current_line_number() and xml_get_current_column_number() together describe the exact spot.

Example 3 — Measure Indentation Depth

Because the column shows how far a tag is indented, you can use it to check how nested a pretty-printed document is.

PHP
<?php
$xml = "<a>\n  <b>\n    <c/>\n  </b>\n</a>";

$parser = xml_parser_create();
xml_set_element_handler(
    $parser,
    function ($parser, $name) {
        echo $name, " -> column ", xml_get_current_column_number($parser), PHP_EOL;
    },
    function ($parser, $name) {
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. Each nested tag is indented by two more spaces than its parent.
  2. The start handler runs once for A, B, and C, in document order.
  3. The self-closing <c/> still triggers a start event at column 4.
  4. The columns 0, 2, and 4 show how deep each tag sits.

Frequently Asked Questions

It returns the zero-based column of the current parser position on the current line. It is useful for pointing to the exact spot of a tag or an error.
It starts at 0. Add 1 if you want to show a column that matches most code editors.
Yes. Every line break resets the column back to 0, so always read it together with xml_get_current_line_number().
Call it inside a handler while parsing, or right after xml_parse() returns 0 to see where the error was found.
No. In PHP 8 the parser is an XMLParser object. Pass the object returned by xml_parser_create(), not a resource.

Next: xml_get_current_line_number()

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

xml_get_current_line_number() 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