PHP libxml_disable_entity_loader() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP, libxml_disable_entity_loader() used to switch off the loading of external XML entities. It was a security helper to protect against XXE attacks.

An external entity is a piece of XML that points to another file or URL. Loading it blindly can leak local files, so older PHP code disabled it. As of PHP 8.0 the function is deprecated because external entities are off by default.

Syntax

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

PHP
$previous = libxml_disable_entity_loader(true);
CallWhat it does
libxml_disable_entity_loader(true)Disables external entity loading and returns the previous setting (deprecated in PHP 8.0).
libxml_disable_entity_loader(false)Re-enables external entity loading. Avoid this unless you fully trust the XML.
libxml_disable_entity_loader()Uses the default value of true and disables loading.

Deprecated as of PHP 8.0. Libxml 2.9.0 and later no longer load external entities by default, so you can simply remove calls to this function. Use libxml_set_external_entity_loader() in PHP 8.2+ if you need custom control.

Library declarations
PHP
libxml_disable_entity_loader(bool $disable = true): bool

Parameters

ParameterDescription
disableOptional. Whether to disable the loading of external entities. The default is true.

Return Value

Return ValueDescription
boolThe previous value of the setting. Calling the function in PHP 8 also raises a deprecation notice.

PHP Examples

Example 1 — Version-Aware Usage

Only call libxml_disable_entity_loader() on old PHP versions. PHP 8 does not need it.

PHP
<?php
if (PHP_VERSION_ID >= 80000) {
    echo "PHP 8+: no need to call libxml_disable_entity_loader().", PHP_EOL;
} else {
    libxml_disable_entity_loader(true);
    echo "PHP 7: entity loader disabled manually.", PHP_EOL;
}

How It Works

  1. PHP_VERSION_ID holds the running PHP version as a number, such as 80300.
  2. On PHP 8 and newer the first branch runs and nothing is disabled manually.
  3. On PHP 7 the second branch disables the loader for safety.
  4. The output shown here is what you see on PHP 8.

Example 2 — Deprecation and Return Value

Calling the function in PHP 8 raises a deprecation notice. The at-sign hides it so we can inspect the return value.

PHP
<?php
$previous = @libxml_disable_entity_loader(true);

echo is_bool($previous) ? "Returned a bool" : "Returned something else", PHP_EOL;
echo "Deprecated since PHP 8.0", PHP_EOL;

How It Works

  1. The at-sign suppresses the deprecation notice that PHP 8 would normally show.
  2. The function returns the previous setting, which is always a boolean.
  3. is_bool() confirms the type of the returned value.
  4. In real projects, remove the call instead of hiding the notice.

Example 3 — Parse Untrusted XML Safely

The modern way to stay safe is to skip risky flags and block network access when parsing.

PHP
<?php
$xml = '<?xml version="1.0"?>
<book><title>Safe Parsing</title></book>';

libxml_use_internal_errors(true);
$book = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NONET);

echo "Title: ", $book->title, PHP_EOL;

How It Works

  1. The XML is a small trusted example, but the pattern works for any input.
  2. LIBXML_NONET blocks network access during parsing.
  3. We do not pass LIBXML_NOENT or LIBXML_DTDLOAD, so external entities are not expanded.
  4. No call to libxml_disable_entity_loader() is needed in PHP 8.

Frequently Asked Questions

Yes. It is deprecated as of PHP 8.0 because external entity loading is disabled by default in libxml 2.9.0 and later. Calling it raises a deprecation notice.
In most cases, nothing. Simply remove the call and avoid LIBXML_NOENT and LIBXML_DTDLOAD on untrusted XML. In PHP 8.2+ you can use libxml_set_external_entity_loader() for custom control.
XXE stands for XML External Entity. An attacker sends XML that points to a local file or URL, hoping your parser loads it and reveals private data. Disabling external entities prevents this.
It returns the previous value of the setting as a boolean. This lets old code restore the earlier state after parsing.
No. It is unnecessary and produces a deprecation notice. If you still support PHP 7, wrap the call in a PHP_VERSION_ID check.

Next: libxml_get_errors()

libxml_get_errors() returns an array of all errors currently stored in the libxml error bu.

libxml_get_errors() 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