149
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..

Charles
  • 50,943
  • 13
  • 104
  • 142
user198729
  • 61,774
  • 108
  • 250
  • 348
  • 3
    If you're using Apache, you might have to restart your server! If you're using XAMPP just select "restart" in the manager app. – st4wik Apr 09 '15 at 13:54
  • 3
    I suggest running `grep display_errors /path/to/php.ini` to see if your line is getting overrided somewhere down the file. This is what was happening to me. – rgajrawala Aug 21 '15 at 18:02
  • 1
    Error_reporting is the solution – David Stienen Jun 16 '17 at 11:08
  • 2
    try `ini_set('display_errors',0)` in your php file. When you ini-settings are not working, you should check if `phpinfo()` shows your desired value. If not you either changed the wrong ini-file or something overwrites your values from php.ini – Radon8472 Nov 23 '19 at 18:51
  • it is no php 5.3 bug. display_errors works from php 4.0.3 up to the latest versions @see https://3v4l.org/87gIl – Radon8472 Nov 23 '19 at 19:03

17 Answers17

98

You can disable notices by setting error reporting level to E_ALL & ~E_NOTICE; using either error_reporting ini setting or the error_reporting() function.

However, notices are annoying (I can partly sympathize) but they serve a purpose. You shouldn't be defining a constant twice, the second time won't work and the constant will remain unchanged!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 7
    +1: I approve of mentioning that he should correct the errors, not turn off the reporting. And you did it in a gentler way than @Jonathan Kuhn :-) – Josh May 19 '10 at 16:06
  • 1
    No,seems this is a bug of php5.3,`display_errors` doesn't work as expected. – user198729 May 20 '10 at 11:17
  • @user try a `phpinfo()` to see whether the setting actually applies. Forgive me, but I strongly doubt that you have found a bug in PHP 5.3 - I will only believe it when I see it :) – Pekka May 20 '10 at 11:36
  • Strange,the `local value` is On,but the `master value` is off,don't know what that means exactly... – user198729 May 20 '10 at 11:40
  • @user do you have a `error_reporting()` command somewhere that overrides the ini setting? – Pekka May 20 '10 at 11:42
  • I've commented those commands.Besides,`error_reporting` and `display_errors` are two different directive,IMO.It should have something to do with `local value`,investigating.. – user198729 May 20 '10 at 11:46
  • Tried,not working.Do you know what does `local value/master value` mean for `display_errors` in `phpinfo()` ? – user198729 May 20 '10 at 11:50
  • 1
    @user I think the local value can come from `ini_set` directives or `php_ini_flag` settings in `.htaccess` files. – Pekka May 20 '10 at 11:53
  • Bingo!!!I want to mark your comment as accepted 100 times! It's in `.htaccess`,never know that ,I only know apache deals with that file,how can it also affect PHP,strange... – user198729 May 20 '10 at 11:55
  • @user ha! That was unexpected. Glad it got sorted out. – Pekka May 20 '10 at 13:05
  • The link for function in answer: http://php.net/manual/en/function.error-reporting.php – akahunahi May 25 '15 at 19:38
  • `error_reporting(E_ALL & ~E_NOTICE);` Add this to top of your PHP script to show (or log) all errors but notices. Notices will not be reported now. – Tarik Nov 07 '16 at 13:31
  • Make sure you run error_reporting(0) before running the code that generates the notices. Regarding redefining constants; This is actually an override(pre) pattern in some applications – vandijkstef Nov 17 '17 at 14:24
  • display_errors and error_reporting is NOT the same. Display_errors controlls if errors are send to stdoutput, but errorhandling still works. But when you change error_reporting to 0, you totally disable all error-handling and errorloging stuff.. you should NEVER to `error_reporting(0)` – Radon8472 Nov 23 '19 at 18:35
42

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.

chown
  • 51,908
  • 16
  • 134
  • 170
Paul Salber
  • 700
  • 6
  • 12
  • 1
    By doing this error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING in the php.ini. It disables the PHP Notice and PHP Warnings. So that no php notice and php warnings are seen in the browsers – Deepak Lamichhane Jul 06 '12 at 07:13
  • This goes for console php settings. If you are using php for generating web pages over apache2 webserver config file is /etc/php5/apache2/php.ini It's best to use phpinfo() to see what config file is actually used. – MilanG Feb 04 '16 at 07:49
  • The only correct answer to the question asked. – Your Common Sense Jan 10 '22 at 19:51
24

Used This Line In Your Code

error_reporting(E_ALL ^ E_NOTICE);  

I think its helf full to you.

Vicky Mahale
  • 1,209
  • 11
  • 18
15

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.

algorhythm
  • 8,530
  • 3
  • 35
  • 47
11

You are looking for:

php -d error_reporting="E_ERROR | E_WARNING | E_PARSE"
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 2
    Hi @Christian, and welcome to StackOverflow! In the future, it would be great if you could add a little extra explanation/detail to your answer to enlighten those who see it as to why/how the solution works/fixes the problem. Thanks so much, and happy coding :) – Zachary Kniebel Oct 07 '14 at 20:14
  • 1
    exactly! I use `/usr/bin/php -d error_reporting="E_ERROR | E_WARNING | E_PARSE" -f /path/to/file.php` for my crontab – vladkras Nov 10 '16 at 14:38
6

For PHP code:

<?php
error_reporting(E_ALL & ~E_NOTICE);

For php.ini config:

error_reporting = E_ALL & ~E_NOTICE
Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81
5

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.

DrLazer
  • 2,805
  • 3
  • 41
  • 52
  • 27
    thats a poor idea usually, its like putting in earplugs because your car is making a horrible grinding noise. – David Morrow May 19 '10 at 21:30
  • 11
    This actually makes perfect sense for some instances, for example when rendering a variable no matter if it is set or empty. – Frans Mar 22 '13 at 15:49
  • 2
    This only makes sense when you have something like a WordPress plugin that the author hasn't updated yet and you know exactly what is causing the problem. It's a band-aid though, not a fix. – Imperative Sep 19 '13 at 02:03
  • 1
    I like both the error_reporting() and '@'. I was not aware of either of these. Thank you. I like the '@' as this provides the same functionality of error_reporting(0) and is easier to type. If the issue is more like "the coat hanger holding up the exhaust" than the "horrible grinding noise", then it allows the project to move forward. I can easily find and fix these when there is time. This helped with an issue where the error is caused by data read from a file, so no typos or library issues. – Mark Longmire Oct 29 '13 at 15:58
  • 2
    Just make sure you put @ where you are 100% sure what you are doing. – Nick May 09 '14 at 07:59
  • Tried all methods mentioned above, this one finally solve my problem of "PHP Notice: Array to string conversion". Thought it is not a solution. But I learn something new to control the error level. Thanks – zhihong Jun 11 '15 at 09:25
  • prepending an @ has the same result like `error_reporting(0)` but only for one line. But be carefull where you use it, because it disables the whole error_reporting, not only the output to screen. – Radon8472 Nov 23 '19 at 19:08
4

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

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • 1
    I needed to use the `ini_set` method mentioned here, the `error_reporting(0)` method mentioned elsewhere did not have any effect. – pix Feb 25 '14 at 02:52
4

error_reporting(E_ERROR); worked for me.

Alex Sorkin
  • 101
  • 3
2

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.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • 1
    If it's a production site, whether or not you think it's error-free, you should still not display errors if they arise - so 'by not causing the errors' seems like a bit of a cheeky response to a valid question. – Cam May 19 '10 at 15:48
  • 5
    since when is it ok to allow errors in a production site? I agree that on a production site you shouldn't display errors, that's not what I was saying. I was saying that you should check if the constant is defined and set it if not (which is why I gave the code sample). – Jonathan Kuhn May 19 '10 at 16:00
  • Although your answer wasn't the "nicest" it seems to answer the OP's question to the fullest. +1 to your answer and /wish it was marked as answer, for it is the correct answer. – Nazca Feb 11 '14 at 18:03
  • @Nazca Yea, I've have since changed my demeanor and try to be much friendlier. :) – Jonathan Kuhn Feb 11 '14 at 20:15
2

If you are running from the command line, you can do this:

php -d display_errors="0" script.php 2>/dev/null

You HAVE to include -d display_errors="0" as well as the redirection of stderr to null, weird

Patrick Steil
  • 453
  • 4
  • 7
  • that works! and I had to send stderr to /dev/null too `php -d display_errors="0" -r 'require "/usr/src/nextcloud/version.php"; echo implode(".",$OC_Version);' 2>/dev/null` – DKebler May 17 '23 at 23:38
1

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

MilanG
  • 6,994
  • 2
  • 35
  • 64
1

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.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
0

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

<?php
if (!defined('MYCONST'))
    define('MYCONST', 'Weeha!');
?>
Thiago Belem
  • 7,732
  • 5
  • 43
  • 64
0

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.

AllenJB
  • 1,245
  • 1
  • 9
  • 18
0

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.

Kevinleary.net
  • 8,851
  • 3
  • 54
  • 46
0

Set the display_errors=Off and restart xampp server

Mohd. Shaizad
  • 91
  • 1
  • 4