PHP

Call to undefined function after a PHP upgrade: removed, or never installed?

The same fatal error has two very different causes after a version change: PHP removed the function, or the extension that provides it was never installed for the new version. Here is how to tell them apart in a minute, and the fix for each.

The host moves the site to a newer PHP version, and one page, or every page, now dies with Fatal error: Uncaught Error: Call to undefined function mb_strlen(). The function existed yesterday. After a version change, that error has two very different causes, and they are fixed in two different places.

What does "Call to undefined function" mean?

It means the code called a function that does not exist in the PHP process that is running it. Since PHP 7, that is an Error exception, and when nothing catches it the request ends with a fatal error. On WordPress you usually see it as the critical error screen, with the full message in the debug log.

A typo in a function name gives the same error, but after an upgrade the code itself has not changed. Something about the PHP running it has.

Was the function removed, or never installed?

Look up the function name on php.net. Every function page shows which PHP versions it exists in, and removed functions say so plainly. That splits the error into two cases:

  • Removed from PHP. The function no longer exists in any build of the new version. The classic examples are mysql_connect() and the rest of the original mysql_* functions, removed in PHP 7.0, and each(), create_function() and money_format(), removed in PHP 8.0. Nothing you install brings them back.
  • Provided by an extension that is not loaded. The function still exists, but it lives in an extension that the new PHP version on your server does not have yet. mb_strlen() needs mbstring, curl_init() needs curl, imagecreatefromjpeg() needs gd, simplexml_load_string() needs xml, and mysqli_connect() needs mysqli.

The second case is more common than people expect. On most Linux servers, extensions are installed per PHP version, so php8.1-mbstring does nothing for PHP 8.3. A host switching your site to 8.3 brings the core of PHP with it, not necessarily every extension the old version had.

Diagram: the two causes of "Call to undefined function" after a PHP upgrade. Removed from PHP: mysql_connect and the other mysql functions were removed in PHP 7.0, each(), create_function() and money_format() in PHP 8.0. The fix is in the code: mysqli or PDO, foreach, an anonymous function. Never installed: mb_strlen needs mbstring, curl_init needs curl, imagecreatefromjpeg needs gd, simplexml_load_string needs xml, mysqli_connect needs mysqli. The fix is on the server: install the extension for the same PHP version the website runs, then reload PHP-FPM.
One error, two fixes: one in the code, one on the server.

Why does it work on the command line but not on the site?

Because the command line and the website are often two different PHP setups on the same server. Running php -m over SSH lists the extensions of the command-line PHP, which may be a different version, with a different php.ini, from the PHP-FPM that serves the website. php --ini shows which configuration files the command line reads.

To see what the website actually has, ask it. A temporary phpinfo() page shows the PHP version and every loaded extension for the web server. Delete it straight after, since it reveals a great deal about the server. Alternatively, run the FPM binary for the site's version directly, for example php-fpm8.3 -m.

How do you replace a removed function?

Change the code to the modern equivalent. The common ones are mechanical:

// each() was removed in PHP 8.0
while (list($key, $value) = each($items)) { ... }
// becomes
foreach ($items as $key => $value) { ... }

// create_function() was removed in PHP 8.0
$double = create_function('$x', 'return $x * 2;');
// becomes
$double = function ($x) { return $x * 2; };

The old mysql_* functions need more than a rename, because the connection is passed differently and queries should move to prepared statements at the same time. Rewrite that code against mysqli or PDO rather than looking for a compatibility shim.

When the call is inside a plugin or a package rather than your own code, update it first. If no updated version exists, the project is probably abandoned, and replacing it is safer than patching it by hand.

How do you install a missing extension?

Install it for the exact PHP version the website runs, then reload PHP-FPM. On Debian and Ubuntu with packaged PHP:

sudo apt install php8.3-mbstring php8.3-curl php8.3-gd
sudo systemctl reload php8.3-fpm

On shared hosting, extensions are usually checkboxes in the control panel, listed separately for each PHP version. In a Docker image built from the official PHP images, add the extension in the Dockerfile with docker-php-ext-install, so every new build has it.

If you cannot install an extension at all, some have pure PHP stand-ins. symfony/polyfill-mbstring, for example, provides the mb_* functions when the extension is missing. It is slower than the real extension and a reasonable stopgap rather than a long-term setup.

Found something wrong in here? Tell me.

Checking API...
Checking AI Gateway...
Check status
⚠️ Setup Required