Diagnosing Common PHP Runtime Errors: Undefined Functions, Memory Exhaustion, and More
Step‑by‑step guide to identify and fix PHP runtime errors—undefined functions, memory limits, syntax errors, and permission issues—using a diagnostic table, ordered checks, and practical verification.
09 Jul 2025, 13:11 UTC

Recognizable Conditions
When a PHP script stops running unexpectedly, the error message is the first clue. Typical symptoms include:
- Fatal error: Call to undefined function – the script attempts to call a function that PHP cannot find.
- Fatal error: Allowed memory size of … exhausted – the script uses more memory than the configured limit.
- Parse error: syntax error – a syntax mistake in a file that is being parsed.
- Warning: failed to open stream: Permission denied – the script cannot write to a file due to OS permissions.
Cause & Diagnostic Table
| Error Message | Common Cause | Diagnostic Check |
|---|---|---|
| Call to undefined function | Missing include/require or autoloader mis‑config | Verify that the file containing the function is loaded before the call. |
| Allowed memory size … exhausted | Large data structures, infinite loop/recursion, or memory‑leaking code | Measure peak memory during execution. |
| Parse error: syntax error | Typo or missing token in an included file | Run php -l file.php to lint the file. |
| Failed to open stream: Permission denied | File or directory permissions prevent write | Check ownership and mode of the target path. |
Ordered Checks
- Validate File Inclusion
Run
php -l script.phpto catch syntax errors before deployment. If the linter reports a problem, fix the syntax and re‑lint. - Test Autoloader Registration
Before using any class, ensure the autoloader is registered. A quick test:
getMessage(); }If the catch block runs, the autoloader is misconfigured.
- Check Memory Usage
During a test run, insert:
Compare
$peaktoini_get('memory_limit'). If the peak approaches the limit, you need to increase it. - Verify File Permissions
Attempt a write in a temporary script:
Check the return value and the error message. If it reports “Permission denied”, adjust the ownership or chmod the directory.
- Inspect Configuration
Run
php -i | grep memory_limitto see the current limit. If you need more memory, editphp.inior addini_set('memory_limit', '512M');at runtime.
Fixes Tied to Findings
- Missing include/require – Add
require_once 'path/to/file.php';before the function call. - Autoloader mis‑config – Ensure
spl_autoload_register()is called early, or use Composer’s autoloader. - Memory exhaustion – Refactor code to process data in chunks, free large variables with
unset(), or increase the memory limit. - Syntax errors – Fix the typo, run the linter again, and redeploy.
- Permission denied – Set correct owner (e.g.,
chown www-data /var/www/html/logs) and mode (e.g.,chmod 755 /var/www/html/logs).
Escalation Criteria
If the error persists after applying the above fixes, consider:
- Reviewing the stack trace for hidden dependencies.
- Checking for PHP extensions that may be missing or incompatible.
- Consulting the PHP error log (
/var/log/php-fpm.logor similar) for additional context. - Engaging a senior developer or system administrator for deeper analysis.
Practical Verification Example
Suppose you receive:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 8192 bytes) in /var/www/html/app.php on line 42
Steps to diagnose and fix:
php -l app.php– no syntax errors.- Run the script with memory profiling:
- If
$peakis close to 256M, increase the limit or refactor. - After adjustment, redeploy and monitor
error_logfor any remaining warnings.
By following the ordered checks, you can pinpoint whether the issue is a configuration problem, code inefficiency, or environment limitation.
Limitations & Caveats
- Do not disable
display_errorsin production; redirect errors toerror_loginstead. - Increasing memory limits can mask underlying inefficiencies; use profiling to confirm necessity.
- File permission changes should be scoped to the application directory to avoid security risks.
- Autoloader failures may be caused by conflicting PSR‑4 namespaces; verify
composer.jsonmapping.
Conclusion
Runtime errors in PHP often stem from simple misconfigurations or overlooked code paths. By using the diagnostic table, performing ordered checks, applying targeted fixes, and knowing when to seek escalation, developers can reduce downtime and improve code reliability.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.