The lines error_reporting(E_ALL);
and ini_set('display_errors', 1);
are among the most common codes used for debugging in a PHP development environment. These two lines ensure that all errors, warnings, and notices that may occur in the PHP script being executed are displayed in the browser.
Usage:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Explanations:
-
error_reporting(E_ALL);
: Enables PHP to display all error types (including warnings, notices, and fatal errors). -
ini_set('display_errors', 1);
: Activates PHP to display these errors on the screen (in the browser). If this value is0
, errors are logged in the background but not reflected in the browser.
When to Use?
-
In the development environment, especially when testing code
-
When quick detection of function or configuration errors is required
-
To see details in session, include, database errors
When Not to Use?
-
Should never be used on live (production) sites. Because errors are shown to the user, which leads to both security vulnerabilities and user experience problems.
Alternative Approach on Live Server:
ini_set('log_errors', 1);
ini_set('error_log', '/home/username/public_html/php-error.log');
error_reporting(E_ALL);
With this structure, errors are not shown to the user but are logged to a specific file.
Activation via .htaccess:
php_flag display_errors On
php_value error_reporting E_ALL
This structure allows direct settings to be made from the .htaccess
file on Apache servers.
WordPress Example:
define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', true);
In WordPress projects, this structure is used in the wp-config.php
file to display PHP errors.
Thanks to this error reporting method, code errors are more easily detected and resolved during the PHP development process. It is especially recommended that these settings be active during the coding phase, but it is mandatory to disable them on live sites.