duaiwo9093 2015-01-06 12:59
浏览 39
已采纳

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>
  • 写回答

1条回答 默认 最新

  • donglun4682 2015-01-06 13:10
    关注

    This should work for you:

    if(isset($ex[1]) && $ex[1] == "265") {
        //do stuff
    
    } else {
        echo "Index doesn't exist!";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?