PHP中的错误/警告消息“PHP通知:未定义的偏移量:1”[重复]

This question already has an answer here:

I am using the below PHP code but it is generating an error/warning message:

PHP Notice: Undefined offset: 1

The code runs as expected.

while(1) {
    while($data = fgets($irc, 128)) {

        $ex = explode(' ', $data);

        if($ex[0] == "PING"){
            fputs($irc, "PONG ".$ex[1]."
");
        }

        if($ex[1] == "265"){
            $lusers = str_replace(',', '', $ex[8]);
            $gusers = $ex[10];
        }

    }
}

I cannot see what is wrong in the code. It is running as expected so why is the error/warning message being generated?

Suggestion to use print_r($ex) shows:

Array
(
    [0] => SeIKVfMCuzNTGjZ

)
PHP Notice:  Undefined offset: 1

Should I just add a check that $ex > 0 or is there a better way?

I updated code so it only runs if $ex > 0:

while(1) {
    while($data = fgets($irc, 128)) {

        $ex = explode(' ', $data);

        if($ex[0] == "PING"){
            fputs($irc, "PONG ".$ex[1]."
");
        }

        if(count($ex) > 0){

            if($ex[1] == "265"){
                $lusers = str_replace(',', '', $ex[8]);
                $gusers = $ex[10];
            }

        }

    }
}
?>

but I still get the error/warning message. How is it possible?

</div>

This should work for you:

if(isset($ex[1]) && $ex[1] == "265") {
    //do stuff

} else {
    echo "Index doesn't exist!";
}