Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

simplexml_load_string() is undefined, or returns false

ErrorFatal error: Uncaught Error: Call to undefined function simplexml_load_string()

Short answer

SimpleXML is on by default, so this error means the PHP you are running was built without it, or its package is not installed (on Debian and Ubuntu it ships in the php-xml package). Check extension_loaded('simplexml') in the same SAPI that fails, install or enable the extension, and restart PHP-FPM or Apache.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask PHP itself whether SimpleXML and its libxml dependency are loaded. Run it with the same binary that fails: the CLI answer can differ from the web server's.

    php -r 'var_dump(extension_loaded("simplexml"), extension_loaded("libxml"));'
  2. 2Match what you see, then jump to its fix

    1. 1extension_loaded("simplexml") prints bool(false) while libxml may be trueThe SimpleXML package is not installed for this PHPFix
    2. 2The CLI check says true, but the error still appears in the browserThe web server runs a different PHP than your terminalFix
    3. 3No fatal error, but the function returns false and raises warningsThe extension is fine, the XML string is notFix

What does the error look like?

  • Fatal error: Uncaught Error: Call to undefined function simplexml_load_string()
  • simplexml_load_string() returns false

Cause 1 of 3

The SimpleXML package is not installed for this PHP

1Check

The manual says SimpleXML is enabled by default and is removed only by the --disable-simplexml build option, so a missing function points to a minimal or custom build. On Ubuntu the module comes from the php8.3-xml package, which the package description lists as DOM, SimpleXML, XML and XSL.

php -m | grep -i -E "simplexml|libxml|dom"
dpkg -l | grep php8.3-xml

2Fix

Install the package for your PHP version. If you compile PHP yourself, rebuild without --disable-simplexml.

sudo apt install php8.3-xml

3Verify

The check from the first step now prints bool(true) twice.

php -r 'var_dump(extension_loaded("simplexml"));'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The web server runs a different PHP than your terminal

1Check

PHP looks for a php.ini per SAPI, so the command line and PHP-FPM or Apache can load different configuration files. Put a one-line script in the web root that prints the answer for the web SAPI.

<?php
echo php_sapi_name(), ' ', PHP_VERSION, ' ',
     var_export(extension_loaded('simplexml'), true);

2Fix

Install and enable the module for the version the web server uses, then restart that service. Remove the test script afterwards.

sudo apt install php8.3-xml
sudo systemctl restart php8.3-fpm

3Verify

Reload the test script. It should print the SAPI name, the version and true.

php --ini

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The extension is fine, the XML string is not

1Check

On bad input the function returns false and raises an E_WARNING for each XML error. A falsy result is not always the boolean false, so compare with ===.

$xml = simplexml_load_string($string);
var_dump($xml === false);

2Fix

Turn on libxml's internal error collection, then print each error with its line. Restore the old setting afterwards.

$previous = libxml_use_internal_errors(true);
$xml = simplexml_load_string($string);
if ($xml === false) {
    foreach (libxml_get_errors() as $error) {
        echo trim($error->message), " (line {$error->line})\n";
    }
    libxml_clear_errors();
}
libxml_use_internal_errors($previous);

3Verify

A broken document now prints readable messages with line numbers instead of bare warnings.

php your-script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Pin the PHP packages in your provisioning script, including php-xml, so a new server has the same modules as the old one.
  • Add an extension_loaded check to your health or deploy script for every extension the app calls.
  • Wrap every XML parse in the libxml error collection above, so bad input from a partner feed is reported, not swallowed.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.