How to show and handle errors in PHP?
When you develop you will definitely want to turn on error reporting in PHP. It
gives you valuable information why something failed. Let’s check some of the most
important error reporting directives in
This sets which errors should be reported. Using
E_ALLis a good practice.
This handles displaying errors to the screen.
This controls reporting errors to a log file. Recommended practice is to always have this enabled.
This defines error log file where errors should be written. It only applies if
Showing errors should depend on the environment your application is present.
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 what went wrong in case of errors.
display_errors = off log_errors = on error_reporting = E_ALL
Error reporting can be also changed with error_reporting() function.
Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all the contributors.