How To Display PHP Errors In The Browser?

Published October 7, 2024

Problem: Hiding PHP Errors in Browser

PHP errors can help with debugging, but they are often hidden in the browser. This can make it hard to find and fix issues in PHP code. Showing these errors in the browser can make debugging easier.

Enabling PHP Error Display

Modifying PHP Code

To show PHP errors in your browser, you can change your PHP code directly:

  • Use the ini_set() function to enable error display:

    ini_set('display_errors', 1);
  • Set the error_reporting level to show all errors:

    error_reporting(E_ALL);
  • Enable display of startup errors:

    ini_set('display_startup_errors', 1);

Add these lines at the start of your PHP script to see errors in the browser.

Tip: Logging Errors

For production environments, it's often better to log errors instead of displaying them. You can do this by adding:

ini_set('log_errors', 1);
ini_set('error_log', '/path/to/error.log');

This will log errors to a file instead of showing them on the screen.

Configuring php.ini File

You can also enable error display by changing the php.ini file:

  • Find your php.ini file. Its location depends on your server setup.

  • Open the file and look for the display_errors directive. Set it to On:

    display_errors = On
  • Adjust the error_reporting level in php.ini:

    error_reporting = E_ALL

Restart your web server after making changes to php.ini.

Using .htaccess for Error Display

If you can't access php.ini, you can use an .htaccess file:

  • Create or open the .htaccess file in your website's root directory.

  • Add this line to enable PHP error display:

    php_flag display_errors on
  • You can also set the error reporting level:

    php_value error_reporting E_ALL

Using .htaccess is useful when you don't have direct access to server configuration files.

Troubleshooting Error Display Issues

Checking Server Configuration

  • Verify Apache settings: Check Apache configuration files (httpd.conf or apache2.conf) for directives that might override PHP error settings. Look for PHP-related settings and make sure they allow error display.

  • Check file permissions: Ensure PHP files have correct read and execute permissions. Incorrect permissions can prevent PHP from running or displaying errors. Use 'chmod 644' for files and 'chmod 755' for directories.

  • Restart web server after changes: After changing configuration files, restart your web server to apply new settings. Use 'sudo service apache2 restart' or 'sudo systemctl restart apache2', depending on your system.

Tip: Check PHP Configuration File

Review your php.ini file for error reporting settings. Look for lines like 'display_errors = On' and 'error_reporting = E_ALL'. Make sure these are set to display errors during development.

Debugging Specific Error Types

  • Handle parse errors: Parse errors occur when PHP can't understand the code syntax. To catch these, add error handling at the top of your script:

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    This will display parse errors in the browser.

  • Address syntax mistakes: Common syntax mistakes include missing semicolons, unmatched brackets, or wrong variable names. Use a code editor with syntax highlighting to spot these errors.

  • Deal with fatal errors: Fatal errors stop script execution. To catch these, you can use a custom error handler:

    <?php
    function custom_error_handler($errno, $errstr, $errfile, $errline) {
      echo "Error: [$errno] $errstr - $errfile:$errline";
      die();
    }
    set_error_handler("custom_error_handler");

    This will display fatal errors in a controlled manner.