Share your love
Best 4 Tips To Hide Warning and Error Messages in PHP
Disable or Hide Warning and Error Messages in PHP is the title of this article to show you how to disable or hide errors on it. PHP is a server-side programming language that is used for web development. One of the important processes in PHP is handling and debugging errors. But seeing the errors directly on the webpage is not looking good and professional. So you can follow the steps below provided by the Orcacore website to hide the error messages in PHP.
Table of Contents
What Are PHP Error Reporting Levels?
To Hide Warning and Error Messages in PHP, we want to understand the error types that can happen in PHP. Here we provide a list to understand them:
PHP Error Type | Description |
E_ERROR | A fatal run-time error that can’t be recovered from. The script execution is halted. |
E_WARNING | Run-time warning that doesn’t stop script execution. |
E_PARSE | Compile-time parse errors. |
E_NOTICE | Run-time notice, indicating something that might be an error. |
E_ALL | All errors and warnings (including E_STRICT and E_DEPRECATED). |
Steps To Disable or Hide Warning and Error Messages in PHP
Now that you understand the PHP error types, you can follow the steps below to control and Hide Warning and Error Messages in PHP.
Step 1 – Control What Kind of Error Messages PHP Should Report
At this point, you can control What Kind of Error Messages PHP Should Report. To do this, you need to use the `error_reporting()` function. For example, you can use the following PHP code to report all errors except notices:
error_reporting(E_ALL & ~E_NOTICE);
Hide All PHP Error Reporting
Also, you can hide all the error reporting by setting this value to 0:
error_reporting(0);
This will hide the errors and prevent them to displayed.
Step 2 – Turn Off Display Error Messages in PHP
At this point, you can use the`ini_set()` function to control whether or not errors should be displayed to the user. For example, if don’t want users to see any error messages, you can use this function with the display_errors option:
ini_set('display_errors', 0);
This will turn off the errors that are printed on the screen.
Step 3 – PHP logging Error Messages
If you want to hide errors from the users but still want to know when they happen, you can use the ‘log_errors’ option. You can set this option to On and set a path file for error logs:
ini_set('log_errors', 'On');
ini_set('error_log', '/path/to/error.log');
Step 4 – Use PHP Try/Catch Blocks To Handle Code Error
At this point, you can use the try-and-catch blocks to handle the errors in PHP. With this option, if the ‘try’ block fails, the code execution will move to the ‘catch’ block.
try {
// Code that might fail
} catch (Exception $e) {
// Code to handle the error
// $e is the exception object, which contains information about the error
}
Also, PHP has several Error Handling Functions that you can use:
- set_error_handler(): Sets a user function to handle errors.
- trigger_error(): Creates a user-level error message.
- error_get_last(): Gets information about the last error that occurred.
- error_log(): Sends an error message to the web server’s error log or a file.
Conclusion
At this point, you have learned to Disable or Hide Warning and Error Messages in PHP. As you saw, PHP provides several handling error functions that you can use to handle your code errors.
Hope you enjoy it. You may like these articles: