1

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!

5 Answers5

5

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 Running
  • 102,619
  • 17
  • 182
  • 182
1

Defining the variable does not mean

define ('SOME_VAR', "with a value")
//or you can add ,true to the end
define ('SOME_VAR2', "with a value",true)//true means case sensitive

The above will set a value that cannot be changed thus it is not really a variable. Eliminating the server error has many ways as the variable is simply not assigned a visable or valid value. Null is a valid value as well as "".

I would suggest try setting the values to null then do your test for not null. Use the isset(){} by itself as the test without the if conditionals before you run and/or wile you run your instal test/loop.

       if (!isset($someVar))
        {
        $someVar = NULL;//or use a "valuestring"
        }

All conditional test, more specifically the isset() statement returns true or false, meaning the test returned true if there was a value. null will usually fix your issue.

BobDCoder
  • 341
  • 2
  • 6
1

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
  • 377,238
  • 77
  • 533
  • 578
0

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
  • 217,595
  • 99
  • 455
  • 496
0

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
 * 
 * https://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;
}
Community
  • 1
  • 1
Frankie
  • 24,627
  • 10
  • 79
  • 121