views:

237

answers:

6

I coming from a J2EE background and it seems that it is very common for PHP developers to turn off and ignore notices with the statement: error_reporting(E_ALL & ~E_NOTICE);

The application that I'm working is full of messages about unset variables? This seems very odd to me.

+9  A: 

It's bad practise, but it's pretty common.

You could say it's considered standard practise since it's the default setting out of the box.

However the fact that it's the default setting in PHP shouldn't be taken as meaning it's a good idea! (cough register_globals cough)

The problem is that E_NOTICE covers both undefined variable and undefined array indexes, the former of which is a far better indication of a bug than the latter.

The classic bug that this hides is using $var when you meant to use $this->var. For this reason alone I think it's worth being anal about cleaning up the undefined array index warning messages so that undefined variable bugs are more obvious.

I had thought that PHP 5.3 allowed you to separate these (I'm not using it yet), but I've just looked and I can't find mention of this.

therefromhere
Good or bad, it *is* standard practice - it's the default in a new PHP install, and the most common setting.
ceejayoz
Edited to clarify that it's the default
therefromhere
Thanks. +1 now. :-)
ceejayoz
A: 

It's considered standard practise since it's the default setting now with the new version of PHP.

You can get away with not setting variables before there use it's common to have a handfull of warnings and therefore the immediate decision (as you know its not going to have a huge effect) is to turn them off.

If you program correctly then it shouldn't be required

I've set all the warning to be off before when i inherited a system because it wasn't feasible to fix there work.

For more info here is the PHP page for error reporting

:)

Andi
A: 

Yes, it is considered standard (opinions on good vs. bad notwithstanding) practice. Per the PHP manual:

In PHP 4 and PHP 5 the default value is E_ALL & ~E_NOTICE. This setting does not show E_NOTICE level errors. You may want to show them during development.

Displaying notices can help the typo prone with debugging, but I can't think of a time I've ever been bitten in the ass by not seeing them (I'm a good typist). I imagine it comes as a shock for someone with a Java background, though...

ceejayoz
Actually it can bite you in the ass a lot, there are lots of times when seeing a notice about an unset variable has helped me trace and find a bug. And its not just due to typos, if you're setting array elements dynamically, there could be a problem with the loop you are using which doesn't set some elements and you'd get a notice level errror on that. So its definitely not a good practice to turn this off during development.
Click Upvote
A: 

There's a reason PHP is telling you what it is, and you should even pay heed to warnings and try to fix them (get it? read the warnings?). This will greatly increase the quality of your final product. :D

CrazyJugglerDrummer
Sometimes that reason is "this could be an issue, but if you know what you're doing you can ignore it". That's essentially the entire *point* of the `E_NOTICE` level of notification, in fact.
ceejayoz
A: 

Not sure if it as mentioned but local development servers like:

  • Xampp;
  • Lampp;
  • Mampp;
  • Wamp;

And many other have PHP error setting to report all errors and notices. Equal to error_reporting(E_ALL);.

However if you ever need these quick codes to check you code for notices or errors, to set the current error report level that the server will perform for a specific PHP script:

Shows Errors but doesn't show Notices:

error_reporting(E_ALL & ~E_NOTICE | E_STRICT);

Shows Everything:

error_reporting(E_ALL);

Shows only Errors:

error_reporting(E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR);

You just have to add one of these lines in your PHP Script on the start.

PS: Not a very good idea to show notices in your hosting server. Be sure to remove these lines when you send then to the hosting server


If your looking to change you PHP.ini config for one of these values.

Open your PHP.ini config file and about the 514º line there is the default error reporting level.

Open this helps.

Regards

EDIT: It was the 514º line not the 504º. Sorry

Fábio Antunes
A: 

The default php.ini file shipped with the official distribution is a bit schizophrenic. On the one hand, it claims to be good for development (e.g. shows errors) but then it has Notices turned off along with a note that turning them on would be good for development. (I actually filed a bug about this, but it was closed as they clearly didn't want to fix this.) IME, Most developers do not change this default because most of them don't know they can. So I see a lot of PHP code that generates Notices for this reason.

If you have a choice, turn Notices on. Then go and fix code that does daft things like use unset variables and what not. Your code will be better for it.

staticsan