How to turn off error reporting in PHP
Overview
It is quite normal for our code to have minor errors. Although such errors do not affect the execution of our code, PHP still displays them. A lot of times, this distorts the design of our websites. In this shot, we'll see how to turn error reporting off in PHP.
Reasons to turn error reporting off
- If the code is in production already, it may be unreasonable for end-users to see errors on the website.
- Normally, error reporting reveals details about the code, which may make it vulnerable to hackers in the long run.
How to turn off error reporting in our code
To successfully turn off error reporting in PHP, we use the error_reporting() function.
Example
<?phperror_reporting(0);trigger_error("user warning!", E_USER_WARNING);echo 'no errors';?>
Explanation
- Line 2: We use
error_reporting(0)to turn off all error reporting in our code
- Line 3: We trigger an error using
trigger_error(“user warning!”, E_USER_WARNING). However, because we have usederror_reporting(0), the error isn't reported.
- Line 4: We display our message without an error.