E_NOTICE is your friend

Errors and error reporting an important part of the development and debugging cycle. In interpreted languages, there are a number of runtime errors that can really help you debug your code. Some languages like python don’t give you a way to make the errors go away, but for better or worse PHP does. While turning off all those annoying E_NOTICE errors can seem tempting, especially when the code still ‘runs’ with them off, its far from a good idea. I’ve recently been working on some code which for historical reasons has E_NOTICE turned off. I’ve been finding that not having notice errors has been nothing but headaches. Turning off E_NOTICE doesn’t seem so terrible at first. What are you really missing, some undefined variable/index messages? Well undefined variables can have dire consequences sometimes.

Show Plain Text
  1. function checkAccess($level) {
  2.     // check that the user is an admin user.
  3.     if ($level == 0) {
  4.         return true.
  5.     }
  6.     ...
  7. }
  8.  
  9. // Call the method
  10.  
  11. if ($this->checkAccess($undefinedVar)) {
  12.     $this->takeTheMoney();
  13. }

While this example maybe a bit exaggerated, it contains a few small mistakes that add up to a huge problem. If notice errors were on, you would find out right away about your mistake and be able to correct it. With E_NOTICE suppressed you might not find out until much later. In the mean time everyone has access to takeTheMoney() which is probably not what you intended. Suppressing notice errors can magnify the consequences any other code might have. Sure the loose comparison might not be a great idea, but its even worse when you don’t know when a null might get accidentally converted into 0. Having E_NOTICE turned off also contributes to a broken window situation where it starts with just E_NOTICE being turned off, and turns into using shut up operators and a whole host of other bad practices. So remember all errors are important even the little ones, they could mutate into bigger ones later on.

Comments

I approve of applications that produce no error output by virtue of having no errors.

Gordon on 4/18/10

I’ve worked both ways for many years and my vote goes for E_NOTICE enabled every time. It saves more time in the long run even if you have to write a few !empty()‘s etc.

Jimmer on 5/15/10

The worst is when you have a constant that you think is defined:

if(CONSTANT) { //do special
}

Not advocating that we should use constants but I’ve seen this happen a few times and it can be a nasty bug. E_NOTICE fixes this as well

Sean on 5/20/10

Comments are not open at this time.