PHP Fatal error: Call to undefined function json_decode()
When your Apache logs start spitting out PHP Fatal error: Call to undefined function json_decode(), the immediate instinct is often to panic about a corrupted installation or a missing system library. However, in many production environments involving legacy stacks, the culprit is rarely the code itself but rather a version mismatch between the CLI interpreter and the server daemon.
The source material indicates a classic symptom of this specific issue: php --version reports PHP 5.5.1, yet the script execution environment fails to recognize json_decode. While phpinfo() confirms that the JSON extension is loaded and lists authors like Omar Kilani and Scott MacVicar, this information can be misleading if you are not examining which binary is being invoked.
The core conflict here lies in how PHP handles multiple installations on the same server. You might have a system-wide PHP installed via your package manager (in this case, a Debian-based distribution given the ~precise tag in the version string) that supports JSON. However, if your application was compiled or linked against a different PHP binaryâperhaps one installed via PECL, an older XAMPP stack, or a custom build from sourceâthat specific binary might be missing the JSON module entirely, even if the system-wide PHP has it.
The error message Call to undefined function is a runtime check. It means the opcode compiler successfully loaded the file, but when looking for the function name in the symbol table during execution, it returned null. This happens strictly because the extension providing that function was not compiled into or dynamically loaded by the specific binary running your script.
To resolve this without blindly upgrading from PHP 5.5 to a modern version (which might break legacy code), you must verify exactly which php executable is handling the request. Run the following command in your terminal:
php -i | grep "Loaded Modules"
If the output does not list json.so or similar JSON-related modules, but your phpinfo() page (accessed via _SERVER['DOCUMENT_ROOT'] or a specific path) shows them, you have confirmed a binary mismatch. The fix involves ensuring that the web server (Apache/Nginx/FastCGI) is configured to use the same PHP binary that your CLI commands invoke.
If you are using PHP-FPM or ModPHP, check your configuration files. For Apache with mod_php, ensure the LoadModule directive points to the correct path and that the PHPIniDir matches the one containing the extension files. In a Debian environment, this often means running:
sudo apt-get install php5-json
or rebuilding the PHP package to include the JSON extension if it was omitted during installation. Always verify that the binary path in your .htaccess or Apache config (AddHandler, SetHandler, or PHPIniDir) aligns with the one you are testing via CLI.
Upgrading PHP is generally the safer long-term strategy, as PHP 5.5 reached end-of-life years ago and no longer receives security patches. If a migration is feasible, plan to move to at least PHP 7.4 or ideally PHP 8.x, where json_decode has been stable for over a decade. However, during the transition, mixing versions can lead to persistent runtime errors if libraries are not recompiled against the new interpreter.
Always validate your environment by running php -m in both CLI and server contexts before deploying code. If the module list differs between the two, investigate your build process or package manager settings immediately.
