Debugging PHP scripts can be challenging if errors are hidden. Enabling the display_errors
directive is a handy way to display errors directly on the web page while testing. This guide will walk you through enabling display_errors
step-by-step in various hosting environments. 🛠️
🔍 What is display_errors
in PHP?
The display_errors
directive in PHP controls whether error messages are shown on the browser. It is a useful feature for debugging during development but should always be disabled in production to avoid exposing sensitive information.
💡 Prerequisites
-
Access to your hosting control panel (cPanel, DirectAdmin, Plesk, etc.) or terminal.
-
Basic knowledge of PHP configurations.
-
A text editor if editing files manually.
📖 Steps to Enable display_errors
⚙️ Option 1: Using the php.ini
File
-
Locate the
php.ini
File:-
In most hosting environments, the
php.ini
file can be found in the root directory or/etc/php/
.
-
-
Edit the File: Open the file in a text editor and find the line:
display_errors = Off
-
Change the Setting: Update the line to:
display_errors = On
-
Restart the Web Server: Execute the following command to apply changes:
sudo systemctl restart apache2
or
sudo systemctl restart nginx
🛠️ Option 2: Using the .htaccess
File
-
Locate the
.htaccess
File:-
Found in your website’s root directory (e.g.,
/public_html/
).
-
-
Add the Directive: Insert the following line:
php_value display_errors On
-
Save and Upload: Save the file and upload it back if edited locally.
🌐 Option 3: Modifying Settings in cPanel
-
Log in to cPanel: Access your hosting account’s cPanel.
-
Navigate to MultiPHP INI Editor:
-
Find the Software section and click MultiPHP INI Editor.
-
-
Select Your Domain:
-
Choose your domain from the dropdown menu.
-
-
Update
display_errors
:-
Locate the
display_errors
setting and set it toOn
.
-
-
Save Changes: Click the Apply button to save.
✨ Option 4: Adding Code in PHP Script
For a temporary solution, add the following lines at the top of your PHP script:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
🛡️ Important Tips
-
Disable
display_errors
in Production: Displaying errors publicly can reveal sensitive server details.display_errors = Off
-
Use Logs for Debugging: Enable error logging instead to track issues without exposing details.
ini_set('log_errors', 1); ini_set('error_log', '/path/to/error.log');
🤔 Troubleshooting Common Issues
-
Changes Not Reflecting:
-
Ensure you’ve restarted the web server.
-
Check for conflicting settings in
.htaccess
or user.ini files.
-
-
Access Denied Errors:
-
Verify file permissions.
-
Ensure your hosting provider allows overriding PHP settings.
-
-
No Errors Displayed:
-
Confirm the script contains errors.
-
Ensure
error_reporting
is set to capture all errors.
-
🎯 Conclusion
Enabling display_errors
is a powerful tool for PHP developers to debug applications efficiently. However, it’s crucial to disable it on live sites and leverage error logging for production environments. By following the steps above, you can easily configure display_errors
in various environments. 🚀
📌 Related Articles
If you found this guide helpful, check out our Knowledgebase for more tutorials. 🌟