[php] How do I turn off PHP Notices?

Notice: Constant DIR_FS_CATALOG already defined

I've already commented out display_errors in php.ini, but is not working.

How do I make PHP to not output such things to browsers?

UPDATE

I put display_errors = Off there but it's still reporting such notices,

Is this an issue with PHP 5.3?

Reporting numerous Call Stack too..

This question is related to php error-handling constants

The answer is


As mentioned by some and if you are the code author, you should correct all those errors, notices, etc. because it will cause more problems for you long terms than not fixing them (especially when you upgrade your OS). For your server, you should have errors displayed in your logs only, not the client's screen.

So to avoid the errors in your browser you use the display_errors flag as you already found:

display_errors = Off

Now the real problem is when you are running someone else code. In that case, modifying the code is likely to get overwritten each time you upgrade that code. It makes it tedious to maintain that code.

In my case, I am running PHP with crontab to have the wp-cron.php script running once in a while. I was getting errors sent to my emails, which becomes tedious when you get one email every 10 minutes! In that case, though, the Wordpress system has a configuration file includes a WP_DEBUG and they call the error_reporting() function so trying to change the error_reporting variable on the command line won't work. Instead you have to edit the wp-config.php file in the root folder and make sure that the WP_DEBUG is set to false. Otherwise you will get all those warnings and notices all the time.


I found this trick out recently. Whack an @ at the start of a line that may produce an warning/error.

As if by magic, they dissapear.


You can set ini_set('display_errors',0); in your script or define which errors you do want to display with error_reporting().


Use phpinfo() and search for Configuration File (php.ini) Path to see which config file path for php is used. PHP can have multiple config files depending on environment it's running. Usually, for console it's:

/etc/php5/cli/php.ini

and for php run by apache it's:

/etc/php5/apache2/php.ini

And then set error_reporting the way you need it:

http://www.phpknowhow.com/configuration/php-ini-error-settings/ http://www.zootemplate.com/news-updates/how-to-disable-notice-and-warning-in-phpini-file


I prefer to not set the error_reporting inside my code. But in one case, a legacy product, there are so many notices, that they must be hidden.

So I used following snippet to set the serverside configured value for error_reporting but subtract the E_NOTICEs.

error_reporting(error_reporting() & ~E_NOTICE);

Now the error reporting setting can further be configured in php.ini or .htaccess. Only notices will always be disabled.


<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

source http://php.net/manual/en/function.error-reporting.php


For PHP code:

<?php
error_reporting(E_ALL & ~E_NOTICE);

For php.ini config:

error_reporting = E_ALL & ~E_NOTICE

From the PHP documentation (error_reporting):

<?php
// Turn off all error reporting
error_reporting(0);
?>

Other interesting options for that function:

<?php

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL & ~E_NOTICE);
// For PHP < 5.3 use: E_ALL ^ E_NOTICE

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

by not causing the errors:

defined('DIR_FS_CATALOG') || define('DIR_FS_CATALOG', 'whatever');

If you really have to, then change error reporting using error_reporting() to E_ALL^E_NOTICE.


Used This Line In Your Code

error_reporting(E_ALL ^ E_NOTICE);  

I think its helf full to you.


Double defined constants

To fix the specific error here you can check if a constant is already defined before defining it:

if ( ! defined( 'DIR_FS_CATALOG' ) ) 
  define( 'DIR_FS_CATALOG', 'something...' );

I'd personally start with a search in the codebase for the constant DIR_FS_CATALOG, then replace the double definition with this.

Hiding PHP notices inline, case-by-case

PHP provides the @ error control operator, which you can use to ignore specific functions that cause notices or warnings.

Using this you can ignore/disable notices and warnings on a case-by-case basis in your code, which can be useful for situations where an error or notice is intentional, planned, or just downright annoying and not possible to solve at the source. Place an @ before the function or var that's causing a notice and it will be ignored.

Here's an example:

// Intentional file error
$missing_file = @file( 'non_existent_file' );

More on this can be found in PHP's Error Control Operators docs.


You are looking for:

php -d error_reporting="E_ERROR | E_WARNING | E_PARSE"

For the command line php, set

error_reporting = E_ALL & ~E_NOTICE

in /etc/php5/cli/php.ini

command php execution then ommits the notices.


I believe commenting out display_errors in php.ini won't work because the default is On. You must set it to 'Off' instead.

Don't forget to restart Apache to apply configuration changes.

Also note that while you can set display_errors at runtime, changing it here does not affect FATAL errors.

As noted by others, ideally during development you should run with error_reporting at the highest level possible and display_errors enabled. While annoying when you first start out, these errors, warnings, notices and strict coding advice all add up and enable you to becoem a better coder.


You can check if the constant's already defined using:

<?php
if (!defined('MYCONST'))
    define('MYCONST', 'Weeha!');
?>

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to error-handling

must declare a named package eclipse because this compilation unit is associated to the named module Error:Failed to open zip file. Gradle's dependency cache may be corrupt What does 'index 0 is out of bounds for axis 0 with size 0' mean? What's the source of Error: getaddrinfo EAI_AGAIN? Error handling with try and catch in Laravel What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean? Raise error in a Bash script Javascript Uncaught TypeError: Cannot read property '0' of undefined Multiple values in single-value context IndexError: too many indices for array

Examples related to constants

Constants in Kotlin -- what's a recommended way to create them? Why Is `Export Default Const` invalid? Proper use of const for defining functions in JavaScript Declaring static constants in ES6 classes? How can I get the size of an std::vector as an int? invalid use of non-static member function Why does JSHint throw a warning if I am using const? Differences Between vbLf, vbCrLf & vbCr Constants Constant pointer vs Pointer to constant Const in JavaScript: when to use it and is it necessary?