Ben Shelock
Ben Shelock

Reputation: 20965

Stop Notices From Displaying In PHP

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

Upvotes: 9

Views: 44414

Answers (6)

user214393
user214393

Reputation: 11

Because i have no php.ini so i just put this tag right after

error_reporting(E_ALL ^ E_NOTICE);

Upvotes: 1

James Hall
James Hall

Reputation: 7693

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.

Upvotes: 0

Jan Jungnickel
Jan Jungnickel

Reputation: 2114

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

Upvotes: 4

Gumbo
Gumbo

Reputation: 655189

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

Upvotes: 8

grawity_u1686
grawity_u1686

Reputation: 16137

Upvotes: 2

sj2009
sj2009

Reputation: 464

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.

Upvotes: 32

Related Questions