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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记