Display All PHP Errors on Screen
When working on a PHP project, I usually log any errors to a file (error log), but on some projects it’s necessary to display any errors right there on the page. Here is the code that I use to make it happen.
Display all errors on screen
Simply add the following three lines to the beginning of your script (or wherever is needed):
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
For more information visit the PHP docs, ini_set() and error_reporting().
Also display parse errors
The above technique doesn’t display parse errors. Unfortunately, to show parse errors on screen, you have to modify your site’s PHP configuration file, php.ini. There, add the following line:
display_errors = on
Note: if the line already exists, just make sure it’s set to on
. Avoid adding multiple instances of the same directive (e.g., display_errors
).
Enable “silenced” errors
If necessary, don’t forget to remove any silencers @
like:
@get_headers($url, true);
With the @
symbol, any errors related to the function will be ignored and not displayed or logged. So just if you want to display any errors from @
-prefixed functions, remove the @
like so:
get_headers($url, true);
Doing that plus adding the above three lines of code will display ALL PHP errors on the page :)