How to Enable Error Reporting in PHP: Simple Steps
To enable error reporting in PHP, use
error_reporting(E_ALL) to report all errors and ini_set('display_errors', '1') to show errors on the screen. Place these lines at the start of your PHP script to see all warnings, notices, and errors during development.Syntax
Use error_reporting() to set which errors PHP reports, and ini_set() to control if errors are displayed. Commonly, E_ALL reports all errors, and 'display_errors' set to '1' shows errors on the page.
php
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?>
Example
This example enables full error reporting and then triggers a warning by dividing by zero. You will see the warning message on the page.
php
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); // This will cause a warning (division by zero) $result = 10 / 0; ?>
Output
<b>Warning</b>: Division by zero in /path/to/script.php on line 7
Common Pitfalls
Many beginners forget to enable display_errors, so errors are logged but not shown. Also, on some servers, display_errors is disabled in php.ini for security. Remember to disable error display on live sites to avoid exposing sensitive info.
php
<?php // Wrong: Only setting error_reporting, no display error_reporting(E_ALL); // Right: Set both error reporting and display error_reporting(E_ALL); ini_set('display_errors', '1'); ?>
Quick Reference
| Setting | Purpose | Example Value |
|---|---|---|
| error_reporting() | Sets which errors to report | E_ALL |
| ini_set('display_errors', '1') | Shows errors on the page | '1' to enable, '0' to disable |
| ini_set('log_errors', '1') | Logs errors to server log | '1' to enable |
| error_reporting(0) | Turns off error reporting | 0 |
Key Takeaways
Use error_reporting(E_ALL) to report all types of errors in PHP.
Enable ini_set('display_errors', '1') to show errors on the screen during development.
Disable error display on live sites to protect sensitive information.
Check your server's php.ini settings if errors do not show as expected.
Always place error reporting code at the start of your PHP scripts.