W3docs

libxml_clear_errors()

Today we're going to discuss the libxml_clear_errors() function in PHP. This function is used to clear any errors that have been generated by the libxml

The libxml_clear_errors() function in PHP clears the internal error buffer populated by libxml-based extensions such as DOMDocument, SimpleXML, and XMLReader. This page explains when libxml collects errors, why you need to clear them, and how to combine libxml_clear_errors() with the other functions in the libxml error API.

Syntax

libxml_clear_errors(): void

The function takes no arguments and returns no value (void). It has been available since PHP 5.1.0 and works in every modern PHP version.

Why You Need to Clear libxml Errors

By default, libxml emits parsing errors as PHP warnings. To handle them in your own code instead, you call libxml_use_internal_errors(true). From that point on, libxml stops printing warnings and silently appends each error to an internal buffer.

That buffer is global and persistent for the request: it keeps growing across every parse or validation you run. If you process several documents in one script — or run a long-lived worker — stale errors from an earlier document remain in the buffer and can pollute the results of libxml_get_errors() and libxml_get_last_error() later on.

libxml_clear_errors() empties that buffer, giving you a clean slate before the next operation.

How to Use libxml_clear_errors()

The typical flow is:

  1. Enable internal error handling with libxml_use_internal_errors(true).
  2. Parse or validate a document.
  3. Read the collected errors with libxml_get_errors() (or libxml_get_last_error()).
  4. Call libxml_clear_errors() to reset the buffer before the next document.

Here is a self-contained, runnable example that loads invalid XML from a string, inspects the errors, and then clears them:

<?php
// 1. Capture libxml errors in the internal buffer instead of emitting warnings.
libxml_use_internal_errors(true);

// Intentionally malformed XML: <title> is closed by </book>, not </title>.
$xml = '<book><title>PHP</book>';

$doc = new DOMDocument();
$doc->loadXML($xml);

// 2. Inspect the errors that were collected.
$errors = libxml_get_errors();
echo "Errors before clearing: " . count($errors) . "\n";
echo "First message: " . trim($errors[0]->message) . "\n";

// 3. Clear the buffer.
libxml_clear_errors();

// 4. The buffer is now empty.
echo "Errors after clearing: " . count(libxml_get_errors()) . "\n";

This prints:

Errors before clearing: 2
First message: Opening and ending tag mismatch: title line 1 and book
Errors after clearing: 0

Validating Against a Schema

libxml_clear_errors() is most useful when you process documents in a loop. After each validation you read the result, then clear the buffer so the next document starts fresh:

<?php
libxml_use_internal_errors(true);

$documents = ['a.xml', 'b.xml', 'c.xml'];

foreach ($documents as $file) {
    $doc = new DOMDocument();
    $doc->load($file);

    if ($doc->schemaValidate('schema.xsd')) {
        echo "$file is valid\n";
    } else {
        // Only the errors for THIS document, because we clear after each one.
        foreach (libxml_get_errors() as $error) {
            echo "$file: " . trim($error->message) . "\n";
        }
    }

    // Reset the buffer before validating the next file.
    libxml_clear_errors();
}

Without the libxml_clear_errors() call at the end of each iteration, errors from a.xml would still appear when you report errors for b.xml.

Common Gotchas

  • Clearing does not disable error collection. libxml_clear_errors() only empties the buffer; libxml keeps collecting new errors as long as libxml_use_internal_errors(true) is in effect.
  • It is global. The buffer is shared across DOMDocument, SimpleXML, and XMLReader. Clearing it affects all of them.
  • Order matters. Always read errors with libxml_get_errors() before you clear — once cleared, they are gone.

Conclusion

The libxml_clear_errors() function provides a straightforward way to manage libxml's internal error buffer. By resetting the buffer after operations like DOMDocument::load() or DOMDocument::schemaValidate(), you prevent stale errors from leaking into later results — which matters most in loops and long-running scripts.

Practice

Practice
What does the 'libxml_clear_errors()' function in PHP do?
What does the 'libxml_clear_errors()' function in PHP do?
Was this page helpful?