When you develop you will definitely want to turn on error reporting in PHP. It
gives you valuable information as to why something has failed. Let's check some
of the most important error reporting directives in php.ini
:
-
error_reporting
This sets which errors should be reported. Using
E_ALL
is a good practice. -
display_errors
This handles displaying errors to the screen.
-
log_errors
This controls reporting errors to a log file. Recommended practice is to always have this enabled.
-
error_log
This defines error log file where errors should be written. It only applies if
log_errors
is enabled.
Showing errors should depend on the environment your application is present.
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
When developing your application locally, you want to show errors on screen and in logs.
display_errors = on
log_errors = on
error_reporting = E_ALL
Be careful when deploying application code online. Disable showing errors on screen for security purposes. You definitely don't want to expose error messages which can contain delicate information about your application to the outside world. However, having logging errors enabled is always useful for information about what went wrong in case of errors.
display_errors = off
log_errors = on
error_reporting = E_ALL
Error reporting can also be changed with the error_reporting() function.
error_reporting(0);
- Error Handling and Logging - A must read PHP manual chapter about configuring the error reporting in PHP.
- Error in PHP - PHP manual errors chapter.
- Monolog - Logging library for PHP.