How to turn off PHP Notices ?

In PHP, Notices are the undefined variables indicated in the PHP project on a set of lines or particular lines. It usually does not affect or break the functionality of the code written. When PHP notices detect errors, it will display like this:

PHP Notice: Use of undefined constant name - assumed 'name' in line number

PHP editing differs from the versions of it. Hence, methods to turn off PHP notices are as follows:

Method 1: It is the most easy and convenient way to turn off the notices. The notices can be disabled through settings the php.ini file. In the current file, search for the line of code error_reporting. There will be a line of Default Value: E_ALL as shown below:



Replace this line of code with Default Value: E_ALL & ~E_NOTICE.

It will display all the errors except for the notices. Make sure the part is enabled and then restart or refresh the server for PHP. In some versions of PHP, the default value is set to Default Value: E_ALL & ~E_NOTICE.

Method 2: To turn the notices off, a line of code can be added to the PHP file, at the beginning of the code of the file. For example gfg.php
The following code would looks something like this

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Open a file
$file = fopen("gfg.txt", "w"
        or die("Unable to open file!");
  
// Store the string into variable
$txt = "GeeksforGeeks \n";
  
// Write the text content to the file
fwrite($file, $txt);
  
// Store the string into variable
$txt = "Welcome to Geeks! \n";
  
// Write the text content to the file
fwrite($file, $txt);
  
// Close the file
fclose($file);
  
?>

chevron_right


Add this line at the start of the code:

error_reporting(E_ERROR | E_WARNING | E_PARSE);
filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
error_reporting(E_ERROR | E_WARNING | E_PARSE); 
  
// Open a file
$file = fopen("gfg.txt", "w"
        or die("Unable to open file!");
  
// Store the string into variable
$txt = "GeeksforGeeks \n";
  
// Write the text content to the file
fwrite($file, $txt);
  
// Store the string into variable
$txt = "Welcome to Geeks! \n";
  
// Write the text content to the file
fwrite($file, $txt);
  
// Close the file
fclose($file);
  
?>

chevron_right


It will display the errors, warnings, and compile-time parse errors.

Method 3: The ‘@’ can be added to any operator to make PHP Notices silent while performing current operation:
@$mid= $_POST[‘mid’];



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.