libxml_use_internal_errors()
Today, we will discuss the libxml_use_internal_errors() function in PHP. This function is used to enable or disable the use of libxml internal errors.
The libxml_use_internal_errors() function lets you tell PHP's libxml-based extensions (DOM, SimpleXML, XMLReader, XMLWriter) to stop printing warnings to the screen and instead collect them in an internal buffer that you can inspect from your own code.
This page explains what the function does, its signature and return value, and how to combine it with the rest of the libxml error API to parse messy XML gracefully.
Why use internal error handling?
By default, when libxml hits malformed XML it raises PHP warnings. In a web app those warnings either clutter your output or get silently swallowed depending on your display_errors/error_reporting settings — and you have no structured way to react to them.
Calling libxml_use_internal_errors(true) changes that:
- Parsing no longer emits PHP warnings. Your script keeps running.
- Every problem is pushed onto an internal buffer as a
LibXMLErrorobject. - You decide what to do — log it, show a friendly message, abort, or ignore non-fatal issues.
This is the standard pattern for parsing XML you don't control (user uploads, third-party feeds, scraped HTML).
Syntax
libxml_use_internal_errors(?bool $use_errors = null): bool$use_errors— passtrueto enable internal error handling,falseto disable it. Passnull(or omit the argument) to query the current state without changing it.- Return value — the function returns the previous setting (a
bool). This lets you restore the old state when you're done, so you don't affect unrelated code.
How to use libxml_use_internal_errors()
Enable internal errors, parse the document, then read the buffer with libxml_get_errors() and clear it with libxml_clear_errors().
The example below parses malformed XML inline (no external file needed, so you can run it as-is):
<?php
// 1. Route libxml warnings into the internal buffer.
libxml_use_internal_errors(true);
// 2. Deliberately broken XML: <from> is never closed.
$xml = <<<XML
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani
</note>
XML;
$doc = new DOMDocument();
$doc->loadXML($xml);
// 3. Inspect what went wrong.
foreach (libxml_get_errors() as $error) {
echo trim($error->message) . " (line {$error->line})\n";
}
// 4. Empty the buffer so later parses start clean.
libxml_clear_errors();
?>Output:
Opening and ending tag mismatch: from line 4 and note (line 5)
Premature end of data in tag note line 2 (line 5)Notice that no PHP warning was printed — the broken document produced two structured error objects instead.
Reading a LibXMLError object
Each item returned by libxml_get_errors() (or libxml_get_last_error()) is a LibXMLError object with useful fields:
| Property | Meaning |
|---|---|
level | Severity: LIBXML_ERR_WARNING, LIBXML_ERR_ERROR, LIBXML_ERR_FATAL |
code | Numeric libxml error code |
message | Human-readable description (often has a trailing newline) |
line | Line number in the source |
column | Column number |
file | File name, or empty string when parsing a string |
You can use level to ignore harmless warnings while still failing on fatal errors:
<?php
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadXML('<a><b></a>'); // mismatched tags → fatal
$error = libxml_get_last_error();
if ($error && $error->level === LIBXML_ERR_FATAL) {
echo "Fatal parse error (code {$error->code})\n";
}
?>Output:
Fatal parse error (code 77)Restore the previous state
Because the function returns the prior setting, a library that flips internal errors on should put it back afterward so it doesn't change behavior for the rest of the application:
<?php
$previous = libxml_use_internal_errors(true);
// ... parse XML and handle libxml_get_errors() ...
libxml_clear_errors();
libxml_use_internal_errors($previous); // restore whatever the caller had
?>Common gotchas
- The buffer is global and persistent. Errors accumulate until you call
libxml_clear_errors()(or the request ends). Always clear it between parses, or you'll re-read stale errors. - It also affects HTML loading.
DOMDocument::loadHTML()reports invalid markup through the same mechanism — handy for scraping imperfect HTML without warnings. - Disabling re-enables PHP warnings. Calling
libxml_use_internal_errors(false)returns libxml to its default of emitting warnings/notices.
Conclusion
libxml_use_internal_errors() is the gateway to robust XML handling in PHP: enable it to silence libxml's default warnings, then use libxml_get_errors(), libxml_get_last_error(), and libxml_clear_errors() to inspect and manage problems on your own terms. Remember its return value so you can restore the previous state, and always clear the buffer between parses.