In this guide, we will show you how to remove warning messages in PHP.
We will also explain why it is generally a bad idea to hide all warning messages.
Hiding all warning messages is basically the same as turning up the music in your car so that you can’t hear the worrying noise that your engine is making.
Hiding PHP warnings with the error_reporting function.
The error_reporting function allows us to tell PHP which errors to report.
For example, if we want to display all error messages except warnings, we can use the following line of code:
//Report all errors except warnings. error_reporting(E_ALL ^ E_WARNING);
Typically speaking, the error_reporting function should be placed at the top of your code. This is because the function can only control errors that occur in the code below it.
Can I hide PHP notice messages as well?
If you also want to hide notice messages, then you can set the following level in the error_reporting function:
//Only report fatal errors and parse errors. error_reporting(E_ERROR | E_PARSE); //This will usually create a division by 0 //warning message. echo 1 / 0; //This will usually create an array to string //notice message. echo array();
In the code snippet above, we told PHP that it should only report fatal errors (E_ERROR) and parse errors (E_PARSE).
Afterwards, we created two lines of code:
- We divided 1 by 0, which would typically result in a “Warning: Division by zero” message.
- We then attempted to echo out an array. Under normal circumstances, this would cause the following notice: “Notice: Array to string conversion”
If you run the example above, you will see that the page doesn’t display any notices or warnings. Furthermore, if you check the PHP error log, you will see that they also haven’t been logged.
Stopping warning messages from being displayed.
If you simply want to stop warning messages from being displayed, but not prevent them from being logged, then you can use the following piece of code:
//Tell PHP to log errors ini_set('log_errors', 'On'); //Tell PHP to not display errors ini_set('display_errors', 'Off'); //Set error_reporting to E_ALL ini_set('error_reporting', E_ALL );
Here, we are using PHP’s ini_set function to dynamically modify the settings in our php.ini file:
- We set log_errors to On, which means that PHP will log warnings to our error log.
- We set display_errors to Off. As a result, PHP will not output errors to the screen.
- Finally, we set error_reporting to E_ALL.
Note: If you can access your php.ini file, then you should probably edit these values directly instead of changing them on the fly.
Using the @ character to suppress errors.
In some cases, you might not have control over certain warnings.
For example, a GET request to an external API could fail, resulting in a “failed to open stream” warning. To prevent this from occurring, we could use the @ character like so:
//API URL $url = 'http://example.com/api'; //Attempt to get contents of that URL $result = @file_get_contents($url);
As you can see, we have placed the @ (AT) character next to our function call. This means that if file_get_contents fails, it will not throw an E_WARNING message.
This works because the @ character is an error control operator that tells PHP to ignore any errors.
Note that error suppression should be used sparingly. Abusing this control operator can lead to issues that are difficult to debug.
Why should I not suppress all warning messages?
An E_WARNING message is an error that does not prevent the rest of your PHP script from executing. Although it does not halt the script, it is still an error. It means that something in your code is not working the way that it is supposed to be working.
You should always try to address the issue instead of hiding it. Otherwise, it may lead to other bugs further down the line.
If you sweep these warning messages under the rug, you will be limiting your ability to spot serious problems in your application.
For example, what if your online shop had a “division by one” error that was preventing certain users from adding multiple items to their basket? Perhaps this issue only occurs when the user applies an expired coupon. Or maybe it happens after they reduce the quantity of an item in their basket.
Either way, something bad is happening and you don’t know about it.
You cannot rely on end users to report issues. Many of them will either think that they are using the website incorrectly or they will simply forget about it and move on. Internet users are impatient. They usually won’t take the time to fill out contact forms or attach screenshots.
As a result, this lazy approach of “nuking” all warning messages has caused your online shop to lose out on sales.