I have a variable in a php code that is in a loop and while the loop is running sometimes becomes undefined. I have an if statement that checks whether the variable is defined so the variable being undefined is not a problem, however, what is a problem are the continuous this variable is undefined messages that are generated by the server appear on my webpage. Is there any way to get rid of these annoying messsages? Thank you!
A:
Put this code at the top of the page to turn off error reporting.
error_reporting(0);
If you just want to hide notice use:
error_reporting(E_ALL ^ E_NOTICE);
But fixing the error would be the smarter thing to do.
John Conde
2010-08-16 19:24:19
A:
Put this line on top of your script:
error_reporting(E_ALL ^ E_NOTICE);
Although it is recommended not to disable that while in development mood.
More Info:
Sarfraz
2010-08-16 19:24:48
A:
This is the function I use. Can be altered to return all kind of things and gives proper credit where credit is due.
/**
* Defines an on the fly alternative to echo content when variable is not available
*
* http://stackoverflow.com/questions/376623/php-printing-undefined-variables-without-warning
*
* @param object $variable
* @param object $default [optional]
* @return
*/
function ifsetor(&$variable, $default = false, $pre = false, $pos = false) {
// if variable is a boolean auto returns the boolean
if(is_bool($variable)) return $variable;
// if variable is string checks if we're adding somehing before or not
if(isset($variable))
$tmp = $pre.$variable.$pos;
else
$tmp = $default;
return $tmp;
}
Frankie
2010-08-16 19:29:24
+3
A:
Define the variable.
Others may tell you to change the error_reporting level, but if your script is throwing Notices you need to fix the errors, not just hide them from view.
Really. Trying to access variables that haven't been defined is a terrible practice that will only lead to more (worse) errors. Fix the cause, not the symptoms.
Jordan
2010-08-16 19:29:46