simplexml_load_string() is undefined, or returns false
Fatal 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.
Fix it fast
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"));'2Match what you see, then jump to its fix
- 1
extension_loaded("simplexml") prints bool(false) while libxml may be trueThe SimpleXML package is not installed for this PHPFix - 2
The CLI check says true, but the error still appears in the browserThe web server runs a different PHP than your terminalFix - 3
No fatal error, but the function returns false and raises warningsThe extension is fine, the XML string is notFix
- 1
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-xml2Fix
Install the package for your PHP version. If you compile PHP yourself, rebuild without --disable-simplexml.
sudo apt install php8.3-xml3Verify
The check from the first step now prints bool(true) twice.
php -r 'var_dump(extension_loaded("simplexml"));'Confirmed by php.net
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-fpm3Verify
Reload the test script. It should print the SAPI name, the version and true.
php --iniConfirmed by php.net
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.phpConfirmed by php.net
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
PHP says curl_init() does not exist because an extension is missing
Fatal error: Call to undefined function mb_strlen()
The function belongs to an extension (curl, mbstring, xml and others) that the PHP process running your code has not loaded.
4 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack 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 fixBack 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