Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should PHP 'Notices' be reported and fixed?

Tags:

php

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?

like image 455
Nicky Hajal Avatar asked May 30 '09 15:05

Nicky Hajal


People also ask

Is PHP notice an error?

Notice ErrorNotice errors are minor errors. They are similar to warning errors, as they also don't stop code execution. Often, the system is uncertain whether it's an actual error or regular code. Notice errors usually occur if the script needs access to an undefined variable.

What are PHP notices?

According to the official PHP website, notices are generated when: “the script encountered something that could indicate an error, but could also happen in the normal course of running a script.”

What is difference between notice and warning in PHP?

NOTICE: It is a message for saying what you should do and what you should not do. WARNING: It occurs at run time. But it do not interrupt Code execution. ERROR: It also occurs at run time, but program execution is not continued it terminates.

How do I stop notices in PHP?

In the current file, search for the line of code error_reporting. There will be a line of Default Value: E_ALL as shown below: Replace this line of code with Default Value: E_ALL & ~E_NOTICE. It will display all the errors except for the notices.


2 Answers

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.

like image 111
Sfynx Avatar answered Oct 01 '22 19:10

Sfynx


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.

like image 29
jalf Avatar answered Oct 01 '22 20:10

jalf