0

I'm receiving the following error in php: Notice: Undefined index: panel_num.

I think I need to use isset() but I can't seem to get it to work with while

global $d;
$i = 1;
while($i <= $d['panel_num']){
$options[] = array(
                      "name" => "Panel".$i,
                        "id" => "panel_".$i,
                       "std" => "",
                      "type" => "panel");
$i++;
}

What is the proper way to resolve this issue?

2
  • The array $d obviously doesn't contain what you expect it to. print_r($d) to see what's actually in there, and if the panel_num key doesn't exist, either bail out of the operation or use a default value instead. Commented Jun 13, 2013 at 14:00
  • 1
    What's in $d? do a var_dump($d) and see what it looks like. Commented Jun 13, 2013 at 14:00

2 Answers 2

1

I think you just need to check for isset() and not empty $d['panel_num']

global $d;
if(isset($d['panel_num']) && !empty($d['panel_num']))
{
    $i = 1;
    while($i <= $d['panel_num']){
    $options[] = array(
                  "name" => "Panel".$i,
                    "id" => "panel_".$i,
                   "std" => "",
                  "type" => "panel");
    $i++;
    }
}

So you will avoid to call your variable if it is not set or it's empty

Sign up to request clarification or add additional context in comments.

Comments

1

Check to see if that variable is set before using it:

global $d;
if (isset($d['panel_num']))
{
  $i = 1;
  while($i <= $d['panel_num']){
  $options[] = array(
                      "name" => "Panel".$i,
                        "id" => "panel_".$i,
                       "std" => "",
                      "type" => "panel");
  $i++;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.