error_reporting configuration
Learn to set which errors are shown with error_reporting and ini_set.
What does error_reporting do?
PHP produces errors at different severity levels: E_ERROR fatal, E_WARNING warning, E_NOTICE notice, E_ALL everything. Each level represents a different seriousness.
error_reporting(E_ALL) chooses which levels are reported. Use E_ALL in development and only E_ERROR in production.
This setting can be changed in php.ini or from inside the script with ini_set().
display_errors and ini_set
error_reporting tells PHP which errors to capture; display_errors decides whether captured errors are printed to the screen. They do different jobs.
ini_set('display_errors', '1') shows errors in development; '0' hides them in production. In production you write errors to a log and hide them from users, because detailed error messages can leak information.
The two settings work together: error_reporting captures, display_errors shows.
Exercise Configure error reporting
Call error_reporting(E_ALL) and ini_set('display_errors', '1'). Then check if (error_reporting() === E_ALL) and print 'E_ALL on'.
Show example solution
Write error_reporting(E_ALL); and ini_set('display_errors', '1');. Then if (error_reporting() === E_ALL) { echo "E_ALL on\n"; }.
Example solution:
<?php
// Set error_reporting and display_errors, then print E_ALL on
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (error_reporting() === E_ALL) {
echo "E_ALL on\n";
}
Test yourself
-
What do you use to show all errors?
- error_reporting(0);
- error_reporting(E_ALL);
- error_reporting(E_ERROR);
- ini_set('errors', 1);
Show answer
error_reporting(E_ALL); — Correct. error_reporting(E_ALL) reports every level.
-
Report all errors
<?php ____(E_ALL); ini_set('display_errors', '1'); $cfg = ['level' => E_ALL]; echo $cfg['level'] . "\n";Show answer
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); $cfg = ['level' => E_ALL]; echo $cfg['level'] . "\n"; -
Match the error levels
- E_ERROR
- Fatal error
- E_WARNING
- Warning
- E_NOTICE
- Notice
- E_ALL
- All errors