PHP libxml_get_errors() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP, libxml_get_errors() returns an array of all errors currently stored in the libxml error buffer. Each item is a LibXMLError object.

Use it after turning on libxml_use_internal_errors(true). It lets you loop through every warning and error to see the message, the line number, and how serious the problem is.

Syntax

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

PHP
$errors = libxml_get_errors();
CallWhat it does
libxml_get_errors()Returns an array of LibXMLError objects, or an empty array if there are none.
count(libxml_get_errors())Returns how many errors are currently stored.
libxml_get_errors()[0]->messageReads the message of the first error (only if the array is not empty).

Each LibXMLError object has these properties: level, code, column, message, file, and line. The level is one of LIBXML_ERR_WARNING, LIBXML_ERR_ERROR, or LIBXML_ERR_FATAL. Messages usually end with a newline, so use trim() before printing.

Library declarations
PHP
libxml_get_errors(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayAn array of LibXMLError objects. The array is empty when no errors were collected.

PHP Examples

Example 1 — Read the First Error

Parse broken XML quietly, then read the line number and message of the first error.

PHP
<?php
libxml_use_internal_errors(true);

$xml = simplexml_load_string('<root><item>Hello</root>');

if ($xml === false) {
    $errors = libxml_get_errors();
    $first = $errors[0];

    echo "Line {$first->line}: ", trim($first->message), PHP_EOL;
    echo $first->level === LIBXML_ERR_FATAL ? "Fatal error" : "Other level", PHP_EOL;
}

How It Works

  1. libxml_use_internal_errors(true) stores errors instead of printing warnings.
  2. simplexml_load_string() returns false because the item tag is never closed.
  3. libxml_get_errors() gives an array and we take the first LibXMLError.
  4. We print its line and trimmed message, then compare the level with LIBXML_ERR_FATAL.

Example 2 — Valid XML and Repeated Reads

Valid XML leaves the buffer empty, and reading errors does not remove them.

PHP
<?php
libxml_use_internal_errors(true);

simplexml_load_string('<ok>Fine</ok>');
echo "Errors for valid XML: ", count(libxml_get_errors()), PHP_EOL;

simplexml_load_string('<a><b></a>');
$a = count(libxml_get_errors());
$b = count(libxml_get_errors());

echo $a === $b ? "Buffer kept after reading" : "Buffer changed", PHP_EOL;

How It Works

  1. A well-formed document adds nothing to the buffer, so the count is 0.
  2. A broken document adds one or more errors.
  3. Calling libxml_get_errors() twice returns the same number of errors.
  4. That proves reading the errors does not clear them.

Example 3 — Find the Line of the Problem

Use the line property to tell users exactly where the XML is broken.

PHP
<?php
libxml_use_internal_errors(true);

$xml = "<root>\n  <item>One</item>\n  <item>Two</root>";
$doc = new DOMDocument();

if (!$doc->loadXML($xml)) {
    $errors = libxml_get_errors();
    echo "Problem on line ", $errors[0]->line, PHP_EOL;
    echo "Total errors found: ", count($errors) > 0 ? "at least 1" : "none", PHP_EOL;
}

libxml_clear_errors();

How It Works

  1. The XML string has three lines, and the last item tag is not closed properly.
  2. DOMDocument::loadXML() returns false for this broken document.
  3. The first error reports line 3, where the mismatch was found.
  4. libxml_clear_errors() cleans up the buffer at the end.

Frequently Asked Questions

It returns an array of LibXMLError objects for every error stored in the libxml buffer. If nothing went wrong, the array is empty.
You probably did not call libxml_use_internal_errors(true). Without it, libxml prints warnings straight to the output instead of storing them in the buffer.
level, code, column, message, file, and line. The level tells you if it is a warning, an error, or a fatal error.
libxml_get_errors() returns all collected errors as an array. libxml_get_last_error() returns only the most recent one, or false if there is none.
No. The errors stay in the buffer until you call libxml_clear_errors() or turn internal errors off.

Next: libxml_get_external_entity_loader()

libxml_get_external_entity_loader() returns the custom function libxml uses to load extern.

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