PHP xml_get_error_code() Function

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

Definition

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

When xml_parse() returns 0, something is wrong with the XML. This function tells you what kind of problem it was, and you can turn the number into text with xml_error_string().

Syntax

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

PHP
$code = xml_get_error_code($parser);
CallWhat it does
xml_get_error_code($parser)Returns the last error code for the parser.
xml_get_error_code($parser) === XML_ERROR_NONEChecks that no error happened.
xml_error_string(xml_get_error_code($parser))Turns the code into a message.

Call it right after xml_parse() returns 0. A value of 0, which is XML_ERROR_NONE, means there is no error.

Library declarations
PHP
xml_get_error_code(XMLParser $parser): int

Parameters

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

Return Value

Return ValueDescription
intThe error code. 0 means no error. Compare it with the XML_ERROR_* constants.

PHP Examples

Example 1 — Read the Code After a Failed Parse

Parse XML with a wrong closing tag and look at the error code.

PHP
<?php
$parser = xml_parser_create();
$result = xml_parse($parser, '<root><item>Tea</root>', true);

echo "Parse result: ", $result, PHP_EOL;

$code = xml_get_error_code($parser);
echo "Error code: ", $code, PHP_EOL;
echo "Is tag mismatch: ", $code === XML_ERROR_TAG_MISMATCH ? "yes" : "no", PHP_EOL;

How It Works

  1. xml_parser_create() makes a new XMLParser object.
  2. xml_parse() returns 0 because </root> closes an open item tag.
  3. xml_get_error_code() returns 7, the number for a mismatched tag.
  4. Comparing with XML_ERROR_TAG_MISMATCH proves it is the expected error.

Example 2 — A Valid Document Has Code 0

When the XML is valid there is no error, and the code is XML_ERROR_NONE, which is 0.

PHP
<?php
$parser = xml_parser_create();
$result = xml_parse($parser, '<root><item id="1">Tea</item></root>', true);

echo "Parse result: ", $result, PHP_EOL;
echo "Error code: ", xml_get_error_code($parser), PHP_EOL;
var_dump(xml_get_error_code($parser) === XML_ERROR_NONE);

How It Works

  1. The XML string is well formed, so xml_parse() returns 1.
  2. xml_get_error_code() returns 0 because nothing went wrong.
  3. XML_ERROR_NONE is the constant for 0.
  4. var_dump() prints bool(true) to confirm there was no error.

Example 3 — Friendly Messages with match

Map error codes to your own beginner-friendly messages using the match expression from PHP 8.

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

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

$code = xml_get_error_code($parser);

$message = match ($code) {
    XML_ERROR_DUPLICATE_ATTRIBUTE => 'An attribute appears twice',
    XML_ERROR_TAG_MISMATCH => 'A closing tag does not match',
    default => 'Some other XML error',
};

echo "Code: ", $code, PHP_EOL;
echo $message, PHP_EOL;

How It Works

  1. The item tag uses the id attribute two times, which XML does not allow.
  2. xml_parse() fails and xml_get_error_code() returns 8.
  3. match compares the code with each XML_ERROR_* constant.
  4. The first matching branch gives the friendly message that is printed.

Frequently Asked Questions

It returns the code of the last error found by an XML parser. It returns 0 when there was no error.
Call it after xml_parse() returns 0. Reading it earlier is not useful because no error has happened yet.
Pass it to xml_error_string(), for example xml_error_string(xml_get_error_code($parser)).
Use the XML_ERROR_* constants such as XML_ERROR_NONE, XML_ERROR_SYNTAX, XML_ERROR_NO_ELEMENTS, and XML_ERROR_TAG_MISMATCH.
No. XML parse problems do not throw. You must check xml_parse() and then read the code yourself.

Next: xml_parse()

xml_parse() feeds XML text to a parser and makes it call your handlers as it finds tags an.

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