tags:

views:

128

answers:

5

I recently switched to a new setup that is reporting PHP Notices; my code has worked fine without these notices being fixed, but I am wondering if it makes sense to fix every one and leave them reported or just ignore them and turn notice reporting off.

What are some different opinions on this? Are there any best practices associated with notices?

A: 

In a production environment you SHOULD NOT display ANY notices.

Even if they crash your application.

It's just a security tip.

If you migrate from an old php version to a recent one, you should at least take a look at the notice, to know what it is about. Sometimes it's about security, sometimes just informe you that this function is deprecated.

You should also take care about the notices, except if you really know what you are doing.

Boris Guéry
+2  A: 

They are called warnings because your program still works - But it means that you did something wrong. Fix them if you got the time to do it and if you want the code to be "clean" and possibly good. Your code is not good if you get warnings.

...and warnings may lead to errors:

If you get an undefined variable warning it means that you did not check for the existence of a variable. What if you use this variable in a MySQL query? It will fail and break your program.

Don't display warnings on a live website. They present parts of your code to the outside.

The best way is to code in a way that avoids warnings. ;)

Lennart
+6  A: 

Yes, I would eliminate notices from any PHP code I write, even if it's just turning off the specific notice ID once I've investigated all instances of it. Notices (i.e. undefined index, initialised variable) very often indicate a situation you've forgotten to anticipate. You should investigate each one, eliminate the real problems, then possibly suppress the others (with a comment stating why).

The PHP manual itself states "NOTICE messages will warn you about bad style", so I guess the official "best practice" would be pretty much what I outlined. Follow good style, unless you have a valid reason not to.

Adam Wright
+2  A: 

It depends on which notices-- most notices are evidence of bad code smells-- undefined array indices etc. If you do suppress a notice, leave a comment next to it indicating why you think it should be suppressed.

PHP.net Says:

Note: Enabling E_NOTICE during development has some benefits. For debugging purposes: NOTICE messages will warn you about possible bugs in your code. For example, use of unassigned values is warned. It is extremely useful to find typos and to save time for debugging. NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as $arr['item'] since PHP tries to treat "item" as constant. If it is not a constant, PHP assumes it is a string index for the array.

I would treat every notice as a free opportunity to improve your code. :)

sj2009
+5  A: 

Errors are errors. They have to be fixed before your code works.

Warnings are warnings. They warn you that what you're doing is probably a bad idea, even if it works (or seems to work) for you at the moment. So they too should probably be fixed.

Notices are notices. They should be noticed. Hence the name. It may not be a problem that your code generates some, but it's something you should examine and judge on a case-by-case basis.

And of course, it is much easier to notice notices if you don't get 400 of them. So there's a big benefit to trying to eliminate them. It makes the ones you haven't yet noticed become more noticeable.

jalf