Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

PHP Says "Call to Undefined Function"? Check the Extension First

You move a site to a new host, or your host upgrades PHP, and a page that worked yesterday dies with one line: PHP Fatal error: Uncaught Error: Call to undefined function mb_strlen() The code did not change. The functi

You move a site to a new host, or your host upgrades PHP, and a page that worked yesterday dies with one line:

PHP Fatal error: Uncaught Error: Call to undefined function mb_strlen()

The code did not change. The function is spelled correctly. So why does PHP suddenly claim it does not exist?

Because mb_strlen() is not part of PHP's core. It belongs to the mbstring extension, and the PHP version your site now runs does not have that extension enabled. Almost every time this error appears out of nowhere, that is the story: the code is fine, and PHP is missing the extension that provides the function.

How to confirm it in ten seconds

Run this from the command line:

php -m

That lists every loaded extension. If the one you need is missing from the list, you have found the cause. The usual suspects: mb_strlen() and mb_substr() need mbstring; curl_init() and curl_exec() need curl; imagecreatetruecolor() needs gd; mysqli_connect() needs mysqli; simplexml_load_string() needs simplexml.

On Ubuntu or Debian, install it and restart the web server:

sudo apt install php-mbstring
sudo systemctl restart apache2

On cPanel hosting, open Select PHP Version, tick the checkbox for the extension, and save. That is the whole fix — no file to edit, no server to restart yourself.

The trap: CLI PHP and web PHP are not the same

This is where most people lose half an hour. php -m over SSH shows the extensions of the command-line PHP. Your website may run a different PHP version through PHP-FPM, with a different php.ini and a different extension list. If you enable mbstring for the CLI version while the site runs another version, nothing changes and the error stays.

Check both: php --ini for the command line, and a small phpinfo() page for the web version. Enable the extension for the version the site actually runs.

When it is not the extension

Three other causes cover nearly everything else. First, a typo — one wrong letter in the function name, which function_exists('mb_strlen') will confirm is not the problem if it returns false for a built-in. Second, a file that was never included: your own helper only exists after its file is loaded, so a missing require_once or a missing require DIR . '/vendor/autoload.php'; at the top of the entry script produces the same error. Third, a function PHP removed: mysql_connect(), each() and create_function() are gone in modern PHP, and no setting brings them back — the code has to use the modern replacement.

There is also the namespace version of this problem. Inside a namespaced file, calling rupiah(15000) makes PHP look for App\rupiah(). If the error message shows a namespace prefix in front of the function name, add a use function import at the top of the file.

The short version

Read the function name from the error message. Run php -m and check the extension for the PHP version your site actually runs. Then check spelling, includes, and the PHP version. In that order, the cause shows up in minutes instead of hours.

I wrote the full guide — all six causes, the five-step debugging checklist, the prevention habits, and the answers to the "works on my computer but not the server" question — here:

https://rajadevsolutions.online/blog/how-to-fix-call-to-undefined-function-error-in-php

It covers the exact error message, working code for every fix, and the table of functions and their extensions, so the next time this error appears you will know which of the six causes it is before you start guessing.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.