PHP libxml_clear_errors() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP, libxml_clear_errors() empties the libxml error buffer. It removes every error that was collected while internal error handling was turned on.

When you call libxml_use_internal_errors(true), XML problems are stored in a list instead of being printed. That list keeps growing, so libxml_clear_errors() lets you start fresh before the next parse.

Syntax

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

PHP
libxml_clear_errors();
CallWhat it does
libxml_clear_errors()Removes all errors from the libxml buffer and returns nothing.
libxml_clear_errors(); libxml_get_errors()Returns an empty array right after clearing.
libxml_use_internal_errors(true); libxml_clear_errors();A common pair: collect errors quietly, then reset the buffer between parses.

libxml_clear_errors() takes no arguments and returns nothing. Call it before each new parse so old errors from a previous document do not get mixed with the new ones.

Library declarations
PHP
libxml_clear_errors(): void

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
voidNo value is returned. The libxml error buffer is simply emptied.

PHP Examples

Example 1 — Clear Errors After a Bad Parse

Collect an error from broken XML, then wipe the error buffer with libxml_clear_errors().

PHP
<?php
libxml_use_internal_errors(true);

simplexml_load_string('<root><item>Hello</root>');
echo "Has errors: ", count(libxml_get_errors()) > 0 ? "yes" : "no", PHP_EOL;

libxml_clear_errors();
echo "After clear: ", count(libxml_get_errors()), PHP_EOL;

How It Works

  1. libxml_use_internal_errors(true) tells libxml to store errors instead of printing warnings.
  2. The XML string is missing a closing item tag, so simplexml_load_string() fails and an error is stored.
  3. libxml_clear_errors() empties the buffer.
  4. libxml_get_errors() now returns an empty array, so count() prints 0.

Example 2 — Errors Pile Up Without Clearing

If you never clear the buffer, errors from each parse are added on top of the old ones.

PHP
<?php
libxml_use_internal_errors(true);

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

simplexml_load_string('<x><y></x>');
$second = count(libxml_get_errors());

echo $second > $first ? "Errors piled up" : "Same count", PHP_EOL;

libxml_clear_errors();
$xml = simplexml_load_string('<ok>Fine</ok>');
echo "Errors after clean parse: ", count(libxml_get_errors()), PHP_EOL;

How It Works

  1. The first broken document adds its errors to the buffer.
  2. The second broken document adds more errors without removing the first ones.
  3. Because nothing was cleared, the second count is bigger than the first.
  4. After libxml_clear_errors(), parsing a valid document leaves the buffer empty.

Example 3 — A Safe Parse Helper

Wrap parsing in a helper that clears old errors first and returns a friendly result.

PHP
<?php
libxml_use_internal_errors(true);

function parseXml(string $xml): string
{
    libxml_clear_errors();

    if (simplexml_load_string($xml) === false) {
        $error = libxml_get_errors()[0];
        return "Invalid: " . trim($error->message);
    }

    return "OK";
}

echo parseXml('<book><title>PHP</title></book>'), PHP_EOL;
echo parseXml('<book><title>PHP</book>'), PHP_EOL;
echo parseXml('<book><title>PHP</title></book>'), PHP_EOL;

How It Works

  1. parseXml() clears the buffer first so earlier failures never leak into the next call.
  2. A valid document returns the word OK.
  3. A broken document returns the first error message from libxml_get_errors().
  4. The third call is OK again because the previous error was cleared.

Frequently Asked Questions

It removes every error from the libxml error buffer. After calling it, libxml_get_errors() returns an empty array and libxml_get_last_error() returns false.
Not really. Without libxml_use_internal_errors(true), libxml prints warnings straight away and there is nothing to collect. The function matters when you store errors in the buffer.
No. It returns void, so there is nothing to check. It always empties the buffer when you call it.
Call it before parsing a new document, or after you finish reading the errors. This keeps messages from different documents separate.
Not exactly. Turning internal errors off also clears the buffer, but it stops libxml from collecting new ones. libxml_clear_errors() only empties the buffer and keeps collecting enabled.

Next: libxml_disable_entity_loader()

libxml_disable_entity_loader() used to switch off the loading of external XML entities. It.

libxml_disable_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