Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Notices From Displaying In PHP

Tags:

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
like image 437
Ben Shelock Avatar asked May 30 '09 17:05

Ben Shelock


People also ask

How do I stop display warnings in PHP?

You can put an @ in front of your function call to suppress all error messages. Sometimes (unfortunately) you really don't have a choice. For example, the PHP function parse_url() generates Warnings for "severely malformed" URLs - which is arguably a bug since the function returns false in this case.

How do I turn off notice?

You can disable push notifications on Android by going into the Settings > Notifications options. Similar to iOS, Android lets you turn off push notifications for individual apps or use a 'Do not disturb' mode.

Can PHP Notice ignore?

You can disable notices by setting error reporting level to E_ALL & ~E_NOTICE; using either error_reporting ini setting or the error_reporting() function.

How do I turn off PHP error reporting?

To turn off or disable error reporting in PHP, set the value to zero. For example, use the code snippet: <? php error_reporting(0); ?>


3 Answers

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.

like image 89
sj2009 Avatar answered Sep 28 '22 04:09

sj2009


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

like image 45
Gumbo Avatar answered Sep 28 '22 02:09

Gumbo


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

like image 30
Jan Jungnickel Avatar answered Sep 28 '22 02:09

Jan Jungnickel