xml_error_string()
The xml_error_string() function is a PHP built-in function that retrieves a string description of an XML parser error. When parsing an XML file using the
The xml_error_string() function is a PHP built-in function that retrieves a string description of an XML parser error. It belongs to the legacy XML Parser extension. When parsing XML files using SimpleXML or other modern libraries, errors are typically handled using libxml_get_errors() instead, as the legacy xml extension is deprecated in modern PHP.
The xml_error_string() function is useful when you need to retrieve a user-friendly error message for legacy XML parsing workflows, though modern applications should prefer libxml_get_errors() for SimpleXML.
Syntax
The syntax of the xml_error_string() function is as follows:
syntax of the xml_error_string() function
xml_error_string(int $error_code): string|falseParameters
$error_code— an integer error code, as returned byxml_get_error_code(). The XML Parser extension exposes these codes asXML_ERROR_*constants (for example,XML_ERROR_NONEis0, meaning "no error").
Return value
xml_error_string() returns a human-readable string describing the given error code (such as "Attribute without value"). If the code is unknown, it returns false. Note that the function only translates a code into text — it does not tell you where in the document the error happened; for that you also need xml_get_current_line_number() and xml_get_current_column_number().
Usage Examples
Let's take a look at some practical examples of using xml_error_string() in PHP.
Example 1: Retrieving an XML Parser Error String (Legacy XML Parser)
The xml_error_string() function works with the legacy XML Parser extension. You can use it to parse XML and retrieve the error string, like this:
parse the XML file and retrieve the error string using xml_error_string() in PHP
$parser = xml_parser_create();
$xml_data = "<invalid xml>";
xml_parse($parser, $xml_data);
$error_code = xml_get_error_code($parser);
if ($error_code !== XML_ERROR_NONE) {
$error_string = xml_error_string($error_code);
echo "Error: $error_string";
}
xml_parser_free($parser);This code creates an XML parser with xml_parser_create(), attempts to parse the malformed string "<invalid xml>" (the xml is read as an attribute name with no value), and checks for errors. xml_get_error_code() returns a non-zero code, xml_error_string() turns that code into text, and the script frees the parser with xml_parser_free().
Output:
Error: Attribute without valueExample 2: Pinpointing Where the Error Occurred
A bare error string rarely helps a developer fix the document. Combine xml_error_string() with the line- and column-number functions to report exactly where the parser stopped:
show the error message together with its location
$parser = xml_parser_create();
$xml_data = "<book>\n <title>Hi</titlex>\n</book>";
if (!xml_parse($parser, $xml_data, true)) {
$code = xml_get_error_code($parser);
printf(
"Error %d: %s at line %d, column %d\n",
$code,
xml_error_string($code),
xml_get_current_line_number($parser),
xml_get_current_column_number($parser)
);
}
xml_parser_free($parser);Here the closing tag </titlex> does not match the opening <title>, so parsing fails on line 2. The location functions report the exact spot:
Output:
Error 76: Mismatched tag at line 2, column 21The exact numeric code and wording depend on your libxml version, but xml_error_string() always returns the matching human-readable message for whatever code xml_get_error_code() reports.
Example 2: Handling SimpleXML Errors (Recommended Modern Approach)
If you are using the SimpleXML library, xml_error_string() is not applicable. Instead, use libxml_use_internal_errors(true) and libxml_get_errors() to handle parsing failures:
Displaying an XML Parser Error Message
libxml_use_internal_errors(true);
if (isset($_FILES["xml_file"])) {
$xml = simplexml_load_file($_FILES["xml_file"]["tmp_name"]);
if ($xml === false) {
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo "Error: " . $error->message;
}
} else {
// process the XML file
}
}This code enables internal error handling for SimpleXML, checks if an XML file was uploaded using the $_FILES array, and attempts to load the file. If an error occurs during parsing, it retrieves the error details using libxml_get_errors() and displays the error message to the user. If no errors occur, the code can process the XML file as needed.
Common XML parser error codes
The XML Parser extension defines named constants for the "no error" and well-formedness cases. The single value you can always rely on is XML_ERROR_NONE (0), which means the parse succeeded. A few common low codes and the strings xml_error_string() returns for them are:
| Code | Message returned by xml_error_string() |
|---|---|
| 0 | No error (matches XML_ERROR_NONE) |
| 1 | No memory |
| 2 | Invalid document start |
| 3 | Empty document |
| 4 | Not well-formed (invalid token) |
| 5 | Invalid document end |
Heads up: the exact integer a real parse returns, and its wording, are produced by the underlying libxml library and vary between PHP/libxml versions — so always pass the live value from
xml_get_error_code()toxml_error_string()rather than assuming a fixed number. Only theXML_ERROR_NONE(0) "success" check is stable across versions.
Comparing against XML_ERROR_NONE (rather than a hard-coded 0) keeps the intent of your success check clear.
Related functions
xml_get_error_code()— returns the numeric code you pass toxml_error_string().xml_parse()— runs the parser and triggers the error you then describe.xml_get_current_line_number()/xml_get_current_column_number()— locate the error in the source.libxml_get_errors()— the modern equivalent used with SimpleXML.
Conclusion
In this article, we've discussed PHP's xml_error_string() function and how it can be used to retrieve a string description of an XML parser error in legacy XML parsing workflows. We've explained what the function does, its syntax, and provided examples of how it can be used. For modern PHP applications using SimpleXML, we recommend using libxml_use_internal_errors(true) and libxml_get_errors() to handle parsing failures, ensuring your web applications are more robust and user-friendly.