Error Reporting in PHP - GeeksforGeeks

Error Reporting in PHP

Last Updated : 2 Nov, 2026

PHP error reporting helps developers identify and debug errors in PHP scripts. You can configure which errors PHP reports and whether those errors appear on the screen or are written to a log file.

  • Error reporting can be configured at the server or script level.
  • PHP supports different error levels, such as warnings, notices, and fatal errors.
  • Error handling settings can be adjusted for development and production environments.

Note: Display errors during development, but disable them on production websites to avoid exposing sensitive information.

1. Enable Error Reporting Using php.ini

The php.ini file contains PHP configuration directives that control error reporting, error display, and error logging.

To enable error reporting:

  • Open the php.ini file.
  • Find the error_reporting directive and set it to E_ALL.
  • Find the display_errors directive and set it to On.
  • Save the file and restart the web server if required by your PHP setup.
error_reporting = E_ALL
display_errors = On
  • error_reporting = E_ALL: Configures PHP to report all supported error levels.
  • display_errors = On: Displays reported errors in the script output.

To disable error display, set display_errors to Off.

display_errors = Off

Note: Disabling error display does not necessarily disable error reporting or logging. For production environments, configure error logging so developers can investigate problems without exposing error details to visitors.

2. Enable Error Reporting Using ini_set()

The ini_set() function changes the value of a PHP configuration directive at runtime, provided that the directive can be changed in that context. The new value applies during the script's execution.

Enable Error Display

Set display_errors to 1 to display errors.

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

function printMyName() {
    echo "My name is GeeksforGeeks";
}

printName();
?>

The error occurs because the script defines printMyName() but calls printName(). PHP function names are case-insensitive, but these names differ beyond capitalization.

Syntax:

ini_set(string $option, string|int|float|bool|null $value): string|false
  • $option: The name of the configuration directive to change.
  • $value: The new value for the directive.

Disable Error Display

Set display_errors to 0 to hide errors from the script output.

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

function printMyName() {
    echo "My name is GeeksforGeeks";
}

printName();
?>

Output:

The function call causes a fatal error, but the error message is not displayed in the response. Depending on the server configuration, the browser may show a generic error page or an HTTP 500 response.

Note: ini_set('display_errors', '1') cannot display errors that occur before the script executes, such as syntax errors that prevent the file from being parsed. Runtime configuration changes also cannot control errors that occur before those changes take effect.

3. Enable Error Reporting Using error_reporting()

The error_reporting() function sets which PHP error levels are reported during script execution. It accepts named error constants or a bitmask combining multiple error levels.

Common PHP Error Constants

ConstantDescription
E_ERRORFatal runtime errors that terminate script execution.
E_WARNINGRuntime warnings that generally do not stop script execution.
E_PARSEErrors encountered while parsing PHP code.
E_NOTICENotices about conditions that may indicate a problem; behavior can vary by PHP version.
E_CORE_ERRORFatal errors during PHP's initial startup.
E_CORE_WARNINGWarnings during PHP's initial startup.
E_COMPILE_ERRORFatal errors encountered during compilation.
E_COMPILE_WARNINGWarnings encountered during compilation.
E_DEPRECATEDNotices about features that may be removed in a future PHP version.
E_USER_ERRORUser-generated fatal errors.
E_USER_WARNINGUser-generated warnings.
E_USER_NOTICEUser-generated notices.
E_USER_DEPRECATEDUser-generated deprecation notices.
E_ALLIncludes all supported error levels covered by this constant.

Example: Using error_reporting()

PHP
<?php
// Report fatal errors and warnings.
error_reporting(E_ERROR | E_WARNING);

// Report all supported error levels.
error_reporting(E_ALL);

// Report all supported error levels except parse errors.
error_reporting(E_ALL & ~E_PARSE);

// Report all supported error levels.
error_reporting(-1);

// Disable reporting through this directive.
error_reporting(0);

echo "Sample error reporting code using error_reporting().";
?>

The final call, error_reporting(0), disables reporting through this directive. The example does not generate an error, so the message is displayed normally.

Syntax:

error_reporting(?int $error_level = null): int

The $error_level parameter specifies which error levels PHP should report. The function returns the previous error reporting level.

Note: error_reporting() controls which errors are reported, whereas display_errors controls whether errors are displayed. These settings serve different purposes.

Configure Error Logging

On production websites, logging errors is generally preferable to displaying them to visitors. PHP provides the log_errors and error_log configuration directives for this purpose.

Add the following settings to php.ini:

error_reporting = E_ALL
display_errors = Off
log_errors = On
  • error_reporting = E_ALL: Reports all supported error levels.
  • display_errors = Off: Prevents errors from being displayed in the response.
  • log_errors = On: Enables logging of PHP errors.

The location of the error log depends on the server configuration and the error_log directive.

Comment