How to enable the "display_errors" option to show PHP error messages Print

  • 0

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

  1. Locate the php.ini File:

    • In most hosting environments, the php.ini file can be found in the root directory or /etc/php/.

  2. Edit the File: Open the file in a text editor and find the line:

    display_errors = Off
  3. Change the Setting: Update the line to:

    display_errors = On
  4. 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

  1. Locate the .htaccess File:

    • Found in your website’s root directory (e.g., /public_html/).

  2. Add the Directive: Insert the following line:

    php_value display_errors On
  3. Save and Upload: Save the file and upload it back if edited locally.

🌐 Option 3: Modifying Settings in cPanel

  1. Log in to cPanel: Access your hosting account’s cPanel.

  2. Navigate to MultiPHP INI Editor:

    • Find the Software section and click MultiPHP INI Editor.

  3. Select Your Domain:

    • Choose your domain from the dropdown menu.

  4. Update display_errors:

    • Locate the display_errors setting and set it to On.

  5. 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

  1. Changes Not Reflecting:

    • Ensure you’ve restarted the web server.

    • Check for conflicting settings in .htaccess or user.ini files.

  2. Access Denied Errors:

    • Verify file permissions.

    • Ensure your hosting provider allows overriding PHP settings.

  3. 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. 🌟


Was this answer helpful?

« Back