User Agents

Web browsers and everyday web tech, explained

How to Enable PHP Errors in Browser

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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 a phpinfo() page if unsure. Run a PHP script with <?php phpinfo(); ?> to find the "Loaded Configuration File" path.
  2. 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.
  3. Find and modify display_errors: Search for display_errors. Set it to On by changing the line to:
    display_errors = On
    If the line is commented out with a semicolon (;), remove the semicolon.
  4. Set display_startup_errors to On: This shows errors that occur during PHP's startup sequence. Locate or add:
    display_startup_errors = On
  5. Ensure error_reporting is comprehensive: Find error_reporting and set it to show all errors:
    error_reporting = E_ALL
    This includes notices, warnings, and deprecation messages.
  6. 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.
  7. 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".
  8. Alternative: Use .htaccess (Apache only): If you can't edit php.ini, add these lines to a .htaccess file in your site's root:
    php_flag display_errors On
    php_flag display_startup_errors On
    php_value error_reporting 32767
    Note: 32767 is the integer value of E_ALL in many PHP versions.
  9. 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 = On and error_log = /path/to/error.log.
  • Check PHP version differences: The value of E_ALL has changed across versions. In PHP 8+, it includes more error types. Using E_ALL is 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 like echo $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_reporting is set to E_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_errors and 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 disable display_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.