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:
<?php
// 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:
<?php
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.