Nicky Hajal
Nicky Hajal

Reputation: 1644

Should PHP 'Notices' be reported and fixed?

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?

Upvotes: 13

Views: 3815

Answers (6)

Sfynx
Sfynx

Reputation: 365

Things like trying to access undefined variables and undefined indexes are serious errors in the book of every serious programmer, so I really don't understand why they chose it to be something you can notice and shrug and walk away. Every other compiler I know would simply abort the program at that exact spot.

I would always fix them, they are almost always a cause of worse errors.

Upvotes: 1

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247999

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.

Upvotes: 22

sj2009
sj2009

Reputation: 464

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. :)

Upvotes: 4

Adam Wright
Adam Wright

Reputation: 49376

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.

Upvotes: 8

Lennart Koopmann
Lennart Koopmann

Reputation: 21059

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. ;)

Upvotes: 2

Boris Guéry
Boris Guéry

Reputation: 47585

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.

Upvotes: 0

Related Questions