开发者

Is it considered standard practice to turn off notices in the error log for PHP development

开发者 https://www.devze.com 2022-12-08 01:10 出处:网络
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:

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_NOTIC开发者_StackOverflow社区E);

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


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.


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


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

:)


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消