-2

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]."\n");
        }

        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]."\n");
        }

        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?

3
  • @Rizier123 I looked at that. I couldn't see how the answer there is relevant to my code here. Commented Jan 6, 2015 at 13:01
  • What is the contents of $irc? Commented Jan 6, 2015 at 13:01
  • 2
    Just do print_r($ex); and you know why you get this warning! Commented Jan 6, 2015 at 13:02

1 Answer 1

0

This should work for you:

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

} else {
    echo "Index doesn't exist!";
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.