How to Enable PHP Errors in Browser
In this article
Enabling PHP errors to display in the browser allows developers to see detailed error messages—like syntax errors, warnings, and notices—directly on the webpage instead of having them hidden or logged only to server files. This is essential during development to quickly identify and fix issues in code. By adjusting PHP configuration settings, you can make errors visible, speeding up debugging. This guide walks you through the process safely and effectively, covering multiple methods depending on your environment and access level.
What You'll Need
- Access to your PHP environment (local server, development server, or hosting control panel)
- Ability to edit PHP configuration files (php.ini, .htaccess, or PHP scripts)
- A text editor or file manager
- Basic understanding of PHP and web server structure
Step-by-Step
- Locate your php.ini file: This is the main PHP configuration file. Its location depends on your server setup. Common paths include
/etc/php/8.1/apache2/php.ini(Linux/Apache),C:\xampp\php\php.ini(Windows/XAMPP), or check via aphpinfo()page if unsure. Run a PHP script with<?php phpinfo(); ?>to find the "Loaded Configuration File" path. - Open php.ini in a text editor: Use a plain text editor like Notepad (Windows), nano (Linux), or VS Code. Avoid word processors like Microsoft Word, which can add formatting.
- Find and modify display_errors: Search for
display_errors. Set it toOnby changing the line to:display_errors = On
If the line is commented out with a semicolon (;), remove the semicolon. - Set display_startup_errors to On: This shows errors that occur during PHP's startup sequence. Locate or add:
display_startup_errors = On - Ensure error_reporting is comprehensive: Find
error_reportingand set it to show all errors:error_reporting = E_ALL
This includes notices, warnings, and deprecation messages. - Restart your web server: Changes in php.ini require a server restart. For Apache, use
sudo systemctl restart apache2(Linux) or restart via XAMPP control panel (Windows). For Nginx with PHP-FPM, restart both services. - Verify changes with a test script: Create a PHP file with intentional errors, such as:
<?php echo $undefined_variable; ?>
Load it in the browser. If configured correctly, you should see a notice like "Undefined variable". - Alternative: Use .htaccess (Apache only): If you can't edit php.ini, add these lines to a
.htaccessfile in your site's root:php_flag display_errors Onphp_flag display_startup_errors Onphp_value error_reporting 32767
Note:32767is the integer value ofE_ALLin many PHP versions. - Alternative: Use PHP code in scripts: Add these lines at the top of individual PHP files during development:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
This method is useful for temporary debugging but should not be used in production.
Common Mistakes
- Editing the wrong php.ini file: Servers may have multiple php.ini files (e.g., CLI vs. web). Always confirm which one is loaded using
phpinfo(). - Forgetting to restart the server: Configuration changes in php.ini do not take effect until the web server is restarted.
- Using display_errors in production: Showing errors to visitors can expose sensitive file paths and code structure. Disable it on live sites.
- Assuming .htaccess works on all servers: Nginx does not support .htaccess. Also, some hosts disable PHP directives in .htaccess for security.
- Overlooking syntax in configuration files: Extra spaces, typos, or missing quotes can prevent settings from applying.
Tips
- Use error_log for production: Instead of displaying errors, log them to a file using
log_errors = Onanderror_log = /path/to/error.log. - Check PHP version differences: The value of
E_ALLhas changed across versions. In PHP 8+, it includes more error types. UsingE_ALLis safer than hardcoded integers. - Enable display_errors selectively: Use conditional logic in PHP scripts to enable error display only for local IPs:
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') { ini_set('display_errors', 1); } - Use development vs production configurations: Maintain separate php.ini files or use tools like Docker to switch settings easily.
- Clear browser cache: After changes, clear your browser cache or test in an incognito window to avoid cached responses.
Troubleshooting
- No errors appear after changes: Verify the correct php.ini is being used via
phpinfo(). Check for typos and ensure the server restarted. Test with a deliberate error likeecho $missing;. - Internal Server Error after .htaccess changes: This often means PHP directives are disabled in .htaccess. Check your server configuration or use php.ini instead.
- Errors show but not all types: Confirm
error_reportingis set toE_ALL. Some hosts override this in master configurations. - Changes revert after server reboot: Some environments (e.g., shared hosting, Docker) regenerate php.ini. Use local PHP script directives or contact your host.
- White screen of death: If the page loads blank, enable
log_errorsand check the error log. A syntax error in php.ini (like a missing semicolon) can break PHP entirely.
Frequently Asked Questions
Why can't I see PHP errors even after enabling them?
Make sure you edited the correct php.ini file used by your web server, not the CLI version. Restart the server and test with a script that triggers a notice.Is it safe to display PHP errors on a live website? No. Displaying errors publicly can reveal file paths, server structure, and code details. Always disabledisplay_errors in production.Can I enable errors without access to php.ini?
Yes. Use ini_set() in your PHP scripts or, on Apache, add directives to .htaccess—if allowed by the server.What does error_reporting = E_ALL mean?
It tells PHP to report all errors, warnings, and notices. This helps catch potential issues early during development.Do these settings work on shared hosting?
It depends. Many shared hosts restrict php.ini edits. Try .htaccess or ini_set() in scripts. Contact support if blocked.How do I turn off PHP errors after debugging?
Set display_errors = Off in php.ini, remove .htaccess directives, or comment out ini_set() lines in your code.