Example 1 — Clear Errors After a Bad Parse
Collect an error from broken XML, then wipe the error buffer with libxml_clear_errors().
<?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
- libxml_use_internal_errors(true) tells libxml to store errors instead of printing warnings.
- The XML string is missing a closing item tag, so simplexml_load_string() fails and an error is stored.
- libxml_clear_errors() empties the buffer.
- libxml_get_errors() now returns an empty array, so count() prints 0.

