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
Turning off error reporing in PHP
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.
RELATED TAGS
php
error_reporting
communitycreator
CONTRIBUTOR
Chinweuba Elijah Azubuike
Can I hide errors and warnings that are displayed on my website?
How to hide PHP errors from your website, using hPanel, .htaccess, and configuration files?
Written by Arnas Damasickis
Updated over a week ago
PHP Errors and warnings can be turned off in several ways:
From hPanel
From WordPress configuration file
From .htaccess
From the PHP file of the specific page
Option 1 - If you are using WordPress
If you are using WordPress, in the File Manager open your public_html/wp-config.php file. Check if it has one of these lines:
define['WP_DEBUG', true];
define['WP_DEBUG', false];
If one of them is present, remove the line. Add the following code:
ini_set['display_errors','Off'];
ini_set['error_reporting', E_ALL ];
define['WP_DEBUG', false];
define['WP_DEBUG_DISPLAY', false];
That's it, now PHP errors will not be displayed on your WordPress website 😊
Opotion 2 - .htaccess and PHP files
In your public_html/.htaccess file [create it, if it's missing in public_html] add this line:
php_flag display_errors off
After this PHP errors will not be displayed on the website.
Option 3 - .php files code
If you want to hide PHP errors from a single session/page of your website, and you know for which PHP page you wish to disable errors display, just add this line to that page's file:
error_reporting[0];
And errors will be hidden 😊
NOTES:
How to turn off PHP error messages in hPanel?
How to turn on PHP error messages in hPanel?
In this article, we’ll show you how to disable the PHP error reporting.
PHP errors are generated by your websites and logged in error_log files in the directory where your PHP files are located. If you do not need the error_log file you can disable PHP error logging.
To turn off PHP error reporting, you need to add the following line to your .htaccess file to suppress any PHP error messages.
php_flag display_errors off
Quick Steps:
- Go to your cPanel account and select File Manager.
- Click on Settings, check the box Show hidden files and click Save.
- Open the .htaccess file and add the code “php_flag display_errors off”.
- Click on Save Changes to save and close the file.
Login into your cPanel account and open File Manager from the Files section.
cPanel home page.
Click on Settings in File Manager.
Settings in File Manager.
Tick the check box “Show hidden files” and click Save.
Settings in File Manager
Select your .htaccess file and click on the Edit option from the dashboard above.
Editing the .htaccess file.
Add the code “php_flag display_errors off” and click Save Changes.
Conclusion
Congrats! Now, you’ve successfully turned off the PHP error reporting using the .htaccess file.
If the steps above listed in this article do not resolve your issue, please feel free to open a support ticket and we’d be happy to take a look.
Our Guiding Principles
- Provide consistent, stable, and reliable web hosting services.
- Ensure rapid ticket response and quick resolutions to issues.
- Never saturate or over-provision servers to ensure stability and speed for our customers.
- Use only high-quality enterprise-class hardware to ensure minimal downtime from hardware failures.
- Provide clear pricing with no hidden fees or gotchas.