PHP libxml_use_internal_errors() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP, libxml_use_internal_errors() turns libxml's internal error handling on or off. When it is on, XML problems are stored in a buffer instead of being shown as PHP warnings.

This is the first step in handling XML errors cleanly. After enabling it, you can use libxml_get_errors() or libxml_get_last_error() to read the problems and show friendly messages.

Syntax

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

PHP
$previous = libxml_use_internal_errors(true);
CallWhat it does
libxml_use_internal_errors(true)Stores errors in the buffer and returns the previous setting.
libxml_use_internal_errors(false)Turns collection off, clears the buffer, and returns the previous setting.
libxml_use_internal_errors()With no argument it just returns the current setting without changing it.

The function always returns the previous setting, so you can save it and restore it later. Turning internal errors off also clears the error buffer.

Library declarations
PHP
libxml_use_internal_errors(?bool $use_errors = null): bool

Parameters

ParameterDescription
use_errorsOptional. true to collect errors in the buffer, false to show them as warnings. If omitted or null, the setting is not changed.

Return Value

Return ValueDescription
boolThe previous value of the internal errors setting.

PHP Examples

Example 1 — Turn Internal Errors On

Enable internal errors and see how the return value reports the previous setting.

PHP
<?php
$previous = libxml_use_internal_errors(true);
echo "Previous: ", var_export($previous, true), PHP_EOL;

$current = libxml_use_internal_errors();
echo "Now: ", var_export($current, true), PHP_EOL;

How It Works

  1. By default libxml prints warnings, so the previous setting is false.
  2. Calling libxml_use_internal_errors(true) switches on error collection.
  3. Calling it again with no argument leaves the setting unchanged.
  4. That second call returns true, the current state.

Example 2 — Turning It Off Clears the Buffer

Disabling internal errors removes anything that was already stored.

PHP
<?php
libxml_use_internal_errors(true);
simplexml_load_string('<root><item></root>');
echo "Errors collected: ", count(libxml_get_errors()) > 0 ? "yes" : "no", PHP_EOL;

libxml_use_internal_errors(false);
libxml_use_internal_errors(true);
echo "After disabling: ", count(libxml_get_errors()), PHP_EOL;

How It Works

  1. Internal errors are on, so the broken XML adds errors to the buffer.
  2. count() shows the buffer has at least one error.
  3. Calling libxml_use_internal_errors(false) clears the buffer.
  4. Turning it back on shows an empty buffer with 0 errors.

Example 3 — Save and Restore the Setting

A polite pattern: remember the old setting, do your work, then put it back.

PHP
<?php
$previous = libxml_use_internal_errors(true);

$xml = simplexml_load_string('<data><value>1</data>');
echo "Parse failed: ", $xml === false ? "yes" : "no", PHP_EOL;

libxml_clear_errors();
libxml_use_internal_errors($previous);

echo "Restored: ", var_export(libxml_use_internal_errors(), true), PHP_EOL;

How It Works

  1. The first line stores the old setting and enables error collection.
  2. The broken XML makes simplexml_load_string() return false without printing a warning.
  3. libxml_clear_errors() empties the buffer.
  4. Passing the saved value back restores the original behavior, which was false.

Frequently Asked Questions

It tells libxml to store XML errors in an internal buffer instead of showing them as PHP warnings. You can then read them with libxml_get_errors() or libxml_get_last_error().
The previous setting as a boolean. You can save it and pass it back later to restore the earlier behavior.
That is the expected result. With internal errors on, warnings are hidden and stored in the buffer. Read them with libxml_get_errors().
Yes. Calling libxml_use_internal_errors(false) clears all collected errors. Use libxml_clear_errors() if you only want to clear while keeping collection on.
Yes. In PHP 8 the parameter defaults to null, which leaves the setting unchanged and simply returns the current value.

Back to libxml Functions

Browse all PHP libxml function tutorials.

libxml functions →

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