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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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.

Solution 6

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply