How to Enable Error Logging in PHP (Step-by-Step Guide for Beginners)

Rajesh Palshetkar
February 23, 2026
How to Enable Error Logging in PHP Step by Step Guide

If you are working with PHP and your website suddenly shows a blank page — commonly known as the "White Screen of Death" — or behaves unexpectedly, hidden errors are usually the cause. Properly enabling PHP error logging allows faster debugging, improved application stability, stronger security, and a more reliable user experience.

By default, many hosting providers disable on-screen error display for security reasons. That is why configuring PHP error logging correctly is essential for both development and production environments.

In this guide, you will learn:

  • What PHP error logging is

  • The types of PHP errors

  • Why error logging is important

  • Three methods to enable error logging

  • Development vs production configuration

  • Best practices for secure logging


What Is PHP Error Logging?

PHP error logging is the process of recording PHP errors, warnings, and notices into a log file instead of displaying them in the browser. This allows developers to debug issues without exposing sensitive system information to users.

Types of PHP Errors

There are four primary types of PHP errors:

  1. Notice – Minor, non-critical issues (e.g., undefined variables). The script continues running.

  2. Warning – Runtime problems that do not stop execution but indicate something is wrong.

  3. Fatal Error – Critical errors that immediately stop script execution.

  4. Parse Error – Syntax errors detected before the script runs.

Understanding this hierarchy helps you prioritize fixes and maintain cleaner, more stable applications.


Why You Should Enable PHP Error Logging

Now that you understand what PHP error logging is and the types of errors it captures, let’s examine why it is crucial in real-world development projects.

Key Benefits

  • Quickly identifies bugs and misconfigurations

  • Prevents unresolved blank page errors

  • Improves debugging efficiency

  • Protects sensitive system information

  • Supports professional development workflows

If you are aiming for Google AdSense approval, maintaining a technically clean and error-free frontend is essential. Error logging helps ensure that users and search engines do not encounter visible system issues.


Method 1: Enable Error Logging Using PHP Code

Add the following code at the top of your PHP file:

<?php
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error_log.txt');
?>

Explanation

  • error_reporting(E_ALL) — Reports all error types

  • ini_set('log_errors', 1) — Enables error logging

  • ini_set('error_log', ...) — Defines the log file location

Once added, PHP will automatically create and write errors to error_log.txt in the specified directory.


Important: Development vs Production Settings

Before configuring server-level settings, you must understand environment differences.

Development Environment

Use visible errors for debugging:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Production (Live Website)

Never display errors publicly. Log them instead:

ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL);

Displaying errors on a live website can expose sensitive server paths, database details, or configuration data.


Method 2: Enable Error Logging via php.ini

If you have access to server configuration, edit the php.ini file:

log_errors = On
error_reporting = E_ALL
error_log = /home/yourusername/public_html/error_log.txt

After saving changes, restart your web server (Apache, Nginx, etc.) for the configuration to take effect.

This method is recommended for full server control and centralized logging.


Method 3: Enable Error Logging via .htaccess (Shared Hosting)

For shared hosting environments, add the following to your .htaccess file:

php_flag log_errors On
php_value error_reporting E_ALL
php_value error_log error_log.txt

Note: This works only if your hosting provider allows PHP directives inside .htaccess.


How to Verify That Error Logging Works

Create a test file:

<?php
echo $undefined_variable;
?>

Open the file in your browser. Then check your error_log.txt file.

If the error appears in the log file, your configuration is working correctly.


Best Practices for PHP Error Logging

  • Store log files outside the public directory whenever possible

  • Restrict file permissions to prevent unauthorized access

  • Avoid displaying detailed errors to users

  • Monitor large log files regularly

  • Rotate or clear logs periodically

  • Use separate logs for large or enterprise projects

Following these practices improves both security and maintainability.


Final Thoughts

Enabling PHP error logging is a foundational step in professional web development. Whether you configure it using PHP code, php.ini, or .htaccess, each method ensures that errors are safely recorded without exposing sensitive details to visitors.

Proper logging helps detect issues early, maintain site performance, and protect your application from unnecessary downtime. For AdSense approval and SEO performance, keeping your frontend technically clean is non-negotiable.

As a best practice, always test logging on a staging or development environment before deploying changes to your live website. Implement it correctly today and build secure, stable, and production-ready PHP applications.


Frequently Asked Questions

PHP error logging is the process of recording errors, warnings, and notices into a log file instead of displaying them on the screen. It helps developers debug issues securely without exposing sensitive information.

You can enable PHP error logging using PHP code with ini_set(), by editing the php.ini configuration file, or by adding directives inside the .htaccess file (if your hosting allows it).

No. On a production website, display_errors should be disabled. Errors should only be logged to a secure file to prevent exposure of sensitive server paths or database details.

The default error log location depends on your server configuration. It is usually defined in the php.ini file or stored inside the server’s log directory.

PHP error logging has minimal performance impact under normal usage. However, very large log files should be monitored and rotated regularly to maintain optimal server performance.
About Rajesh Palshetkar

Professional Strategist at DigitalMotions.

Need a Professional Website?

We provide modern, fast, and high-converting web design services at DigitalMotions.in. Start your digital journey with us today!

SEO Friendly
Fast Loading
Mobile Responsive