- How to remove warnings in PHP.
- Hiding PHP warnings with the error_reporting function.
- Can I hide PHP notice messages as well?
- Stopping warning messages from being displayed.
- Using the @ character to suppress errors.
- Why should I not suppress all warning messages?
- How to Turn Off Notices in PHP
- Using the php.ini File Settings
- Adding Code to the PHP File
- Adding @ to an Operator
- Notices in PHP
- Hide or Show Error Messages in PHP
- Toggling errors in php.ini
- Using htaccess
- WordPress. How to hide PHP warnings and notices
How to remove warnings in PHP.
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.
How to Turn Off Notices in PHP
The undefined variables, specified in a PHP project on a particular line or a set of lines are called notices. As a rule, they don’t break the code functionality.
When detected errors are noticed by PHP, the following notice is displayed:
PHP Notice: Use of undefined constant name - assumed 'name' in line number
However, sometimes developers want to turn off such notices. Here, we will show you how to act in such cases.
Using the php.ini File Settings
The simplest and most convenient way to turn off notices is through the php.ini file settings. All you need to do is searching for the line of code error_reporting . Here, you will see a line of Default Value: E_ALL.
You need to replace it with Default Value: E_ALL & ~E_NOTICE .
All the errors excluding the notices will be displayed by it. Make sure to have enabled that part, then refresh or restart the server.
Adding Code to the PHP File
Here, you will get acquainted with an alternative method to turn off the PHP notices. You can just add a line of code at the beginning of the code of your file. For instance, wdd.php.
Your code will look as follows:
// Open a file ($file = fopen("wdd.txt", "w")) or die("Unable to open file!"); // Storing the string into a variable $txt = "W3docs \n"; // Write the text content to the file fwrite($file, $txt); // Storing the string into variable $txt = "Welcome to W3docs! \n"; // Writing the text content to the file fwrite($file, $txt); // Closing the file fclose($file);
You should add the line below at the beginning of your code:
error_reporting(E_ERROR | E_WARNING | E_PARSE);
Here is how it will look like:
error_reporting(E_ERROR | E_WARNING | E_PARSE); // Open a file ($file = fopen("wdd.txt", "w")) or die("Unable to open file!"); // Storing the string into a variable $txt = "W3docs \n"; // Writing the text content to the file fwrite($file, $txt); // Storing the string into variable $txt = "Welcome to W3docs! \n"; // Writing the text content to the file fwrite($file, $txt); // Closing the file fclose($file);
After running the code above, you will see the warnings, the errors, as well as compile-time parse errors.
Adding @ to an Operator
If you want to make PHP notices silent during the ongoing operation, then add @ to an operator as follows: @$mid= $_POST[‘mid’];
Notices in PHP
In PHP, the notices are known as “soft errors”. They pop up in the logs during different development stages after enabling error logging ( same as debugging) in the php.ini configuration.
Sometimes, developers find the notices and the warnings the same. Yet, they are different: notes are a kind of advisory messages, while warnings tell you that you are doing something wrong, which can lead to further errors.
However, we recommend you take both the notices and the warnings seriously.
Hide or Show Error Messages in PHP
How to show or hide error messages in PHP. There are several ways to do this; from within the PHP scripts themselves, from php.ini, or from changing Apache configuration files.
There are a few ways to control error reporting for PHP scripts; often you would simply configure how to handle errors from within php.ini, but it is also possible to enable and disable errors from individual PHP scripts.
The error_reporting function controls the level of error reporting; you can either use a combination of values or a single value. Possible options include:
To disable errors from a PHP script, you may place the following somewhere towards the top:
ini_set('display_errors', 0); ini_set('display_startup_errors', 0); error_reporting(-1);
Errors should be disabled when using a custom error handler.
To enable errors, you may instead use the following:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
When using a combination of values, each value should be separated by a pipe (|) character:
error_reporting(E_NOTICE | E_WARNING);
Finally, to only exclude a single error type, use the tilde (~) character in front of the type you want to exclude:
Toggling errors in php.ini
How you choose to control errors is not usually important, but doing it from PHP is more friendly towards other developers, since they will more easily be able to make changes.
Sometimes it may make more sense to disable errors from within php.ini instead of attempting to toggle errors on/off from individual PHP scripts. It is recommended to disable errors in production environments, and only enable them doing development. Developers should have full control over their development environment, and therefor it is best to control error reporting from individual scripts or from php.ini directly — avoid using htaccess as much as possible.
Note. You can skip to step 2. if you already know where to find php.ini.
1. Errors can be toggled by adjusting the relevant settings in the php.ini file — the difficult part is to find the right .ini file to edit.
PHP will often have multiple versions installed; this possibly happens after updating, and might, for example, cause both PHP 7.3 and PHP 7.4 to be installed. The file you need to change is usually the latest version; but sometimes PHP-FPM might not load the correct version — that is a separate issue, and not something I will explain how to fix in this article.
But, how to approach it will mainly depend on how you installed PHP and which operating system you are using. For Debian and Ubuntu based Linux distributions, the php.ini file may be located in /etc/php/7.4/apache2/ if using the Apache module, or /etc/php/7.4/fpm/ if using PHP-FPM.
If you are having trouble finding the right file, you can create a script with the following content:
Open the file in your browser and search for «php.ini» on the page. It should be located in the «Loaded Configuration File» row of the table.
2. Once you have located the file, you can change the following values to enable errors:
display_errors = on display_startup_errors = on error_reporting = E_ALL
Or, if you want to disable errors:
display_errors = off display_startup_errors = off error_reporting = -1
Using htaccess
If you are using the Apache HTTP server, you will also be able to toggle errors from .htaccess files. It is often recommended to disable htaccess in the server configuration in order to speed up the server, but this is probably not going to matter much for most people.
Toggling PHP errors from htaccess is done using php_flag in front of the php.ini option you want to change.
To turn on errors in htaccess:
php_flag display_startup_errors on php_flag display_errors on
And to turn off errors in htaccess:
php_flag display_startup_errors off php_flag display_errors off
The error reporting level is not easily controlled with htaccess.
WordPress. How to hide PHP warnings and notices
This tutorial will show you how to hide PHP warnings and notices in WordPress.
For example this warning at the frontend :
- Log into the Host Control Panel ( Cpanel ). Go to File Manager (or you can connect to FTP ): Get to the root directory of the website , open wp-config.php file:
In this file, we will search for:
We will replace this piece of codes with:
ini_set('log_errors','On'); ini_set('display_errors','Off'); ini_set('error_reporting', E_ALL ); define('WP_DEBUG', false); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false);
Save the file.
Refresh the page to check the result.
Feel free to check the detailed video tutorial below: