PHP Notice: Undefined offset: 1 in...” exlode


  • QA Engineer

    I read a lot of things about this mistake, but I never understood how to fix it.

    foreach($data as $date => $stat):
        $from = htmlspecialchars(trim(explode(" - ",$date)[0]));
        $to = (trim(explode(" - ",$date)[1])=='') ? $from : htmlspecialchars(trim(explode(" - ",$date)[0]));
        $html .= "<tr><td colspan='6'><hr></td></tr>\r\n";
    

    error in line 207

    $to = (trim(explode(" - ",$date)[1])=='') ? $from : htmlspecialchars(trim(explode(" - ",$date)[0]));
    


  • Thank you all. The code is as follows:

    foreach($data as $date => $stat):
        if(isset($date[1])){
            $to = htmlspecialchars(trim(explode(" - ",$date)[0]));
            $from = htmlspecialchars(trim(explode(" - ",$date)[0]));
        }else{
            $to = htmlspecialchars(trim(explode(" - ",$date)[1]));
        }
    }
    

    In the first code, I used a non-existent variable. $date[1]


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2