Search code examples
phpwhile-loopundefined-index

Notice: Undefined index with while loop


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?


Solution

  • 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