tags:

views:

326

answers:

5

I want my notices to stop displaying in PHP. Theres no errors in the code, it just says things like undefined index. Which nothing can be done about.

So how do I stop it from displaying?

Notice: Undefined variable: username in C:\wamp\www\watchedit\includes\config.php on line 37

Notice: Undefined variable: key in C:\wamp\www\watchedit\includes\config.php on line 42
+5  A: 

This will turn off notices for the environment programmatically-- from PHP.net.

// Report all errors except E_NOTICE   
error_reporting(E_ALL ^ E_NOTICE);

In some places, you can prefix the statement with "@" and it will silence just that location if it causes a notice.

sj2009
to not display any errors, warning or notices use: error_reporting(0);
Pim Jager
Great option if you absolutely need nothing to appear on a user's screen if something goes wrong. Those tend to cause weird support calls.
sj2009
I assume that one then implements their own error handling methods which show more user friendly error messages than ones about undefined variables..
Perspx
Notices are not problems with the code though, just suggestions. I don't ignore errors.
Ben Shelock
Better: error_reporting(E_ALL
Patrick Daryll Glandien
+2  A: 
grawity
+3  A: 

You should check with isset if the variable exists before trying to read its value.

Gumbo
Or array_key_exists
jmucchiello
+3  A: 

Which nothing can be done about.

This is not true in most cases. Undefined variables can be declared, undefined indices can be tested for using isset(mixed...).

Also, you should configure your environment as suggested above using error_reporting(...). In production environments it is also recommended to disable display_errors

Jan Jungnickel
totally... This question, and some of these answers, are why php has such a bad reputation for code quality...
dicroce
+1  A: 

Striving to generate no notices is a healthy goal, since they will then begin to flag up potential errors or problems. You can write your own error handler to log these, since they should (hopefully) be few and far between.

James Hall