PHP xml_error_string() Function

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

Definition

In PHP, xml_error_string() turns an XML error code into a short, readable message such as "mismatched tag" or "syntax error".

The XML parser reports problems as numbers. xml_get_error_code() gives you that number after xml_parse() fails, and xml_error_string() translates it into text you can show to a user or write to a log.

Syntax

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

PHP
$message = xml_error_string(XML_ERROR_SYNTAX);
CallWhat it does
xml_error_string(XML_ERROR_SYNTAX)Returns the text for a known error code (here: "syntax error").
xml_error_string(xml_get_error_code($parser))The usual pattern: read the code from the parser, then turn it into a message.
xml_error_string(9999)Returns null because the code is not a known XML error.

xml_error_string() does not need a parser. It only translates a number into text. In PHP 8 it returns null when the code has no description.

Library declarations
PHP
xml_error_string(int $error_code): ?string

Parameters

ParameterDescription
error_codeRequired. The error code number, usually from xml_get_error_code() or an XML_ERROR_* constant.

Return Value

Return ValueDescription
stringA short description of the error, for example "mismatched tag".
nullReturned when the error code is not known.

PHP Examples

Example 1 — Read Messages for Known Codes

Pass a few XML_ERROR_* constants to xml_error_string() and print the message for each one.

PHP
<?php
$codes = [XML_ERROR_SYNTAX, XML_ERROR_NO_ELEMENTS, XML_ERROR_TAG_MISMATCH];

foreach ($codes as $code) {
    echo $code, ": ", xml_error_string($code), PHP_EOL;
}

How It Works

  1. The array holds three built-in error constants. Each one is just a number.
  2. foreach loops over the codes one by one.
  3. xml_error_string() looks up the text for each code.
  4. echo prints the number, a colon, and the message on one line.

Example 2 — Unknown Codes Return null

A made-up error code has no message, so xml_error_string() returns null. Use the null coalescing operator to give a friendly fallback.

PHP
<?php
var_dump(xml_error_string(9999));

echo xml_error_string(9999) ?? "Unknown error code", PHP_EOL;

How It Works

  1. 9999 is not an XML error code, so there is no message for it.
  2. var_dump() shows the result is NULL, not an empty string.
  3. The ?? operator replaces null with a friendly fallback text.
  4. echo prints the fallback message.

Example 3 — A Small Validation Helper

Wrap the create, parse, and error steps in one function that returns either OK or a readable error message.

PHP
<?php
function checkXml(string $xml): string
{
    $parser = xml_parser_create();

    if (xml_parse($parser, $xml, true) === 1) {
        return "OK";
    }

    return "Error: " . xml_error_string(xml_get_error_code($parser));
}

echo checkXml('<root><item>Tea</item></root>'), PHP_EOL;
echo checkXml('<root><item>Tea</root>'), PHP_EOL;
echo checkXml('<root></root><extra/>'), PHP_EOL;

How It Works

  1. xml_parser_create() makes a new XMLParser object for each document.
  2. xml_parse() returns 1 when the document is valid, so the helper returns OK.
  3. When parsing fails, xml_get_error_code() reads the error number from the parser.
  4. xml_error_string() turns that number into the message that is returned.

Frequently Asked Questions

It converts an XML error code into a short text message. For example, code 7 becomes "mismatched tag". It is mainly used together with xml_get_error_code().
Call xml_get_error_code($parser) after xml_parse() returns 0. You can also pass one of the XML_ERROR_* constants directly.
No. It only takes an integer error code, so you can call it anywhere without an XMLParser.
It returns null when the code is not a known XML error. Code 0 (no error) also has no message in Expat, so check for null before printing.
It is better to log them and show a simple message such as "The file is not valid XML". The Expat messages are short and technical, which is fine for developers but confusing for most users.

Next: xml_get_current_byte_index()

xml_get_current_byte_index() tells you the byte position the XML parser has reached in the.

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