undefined offset even though it`s defined

748 views php

$a = array("rob", "kate", "alex", "nick");
$q = (isset($_REQUEST["q"]) ? $_REQUEST["q"] : null);
$arr =  explode(".", $q);

$output = array();
foreach ($a as &$value) {
    if (substr($value, 0, strlen($arr[1])) === $arr[1])

I get an undefined offset error inside this part of the code:

($arr[1])) === $arr[1])

Both the $arr[1]'s, even though I have defined them.

Why do I still get this message?

Error Message:

Notice: Undefined offset: 1 in D:\domain.php on line 10

answered question

Pls provide the error message (Stack trace)

Show us the dumped contents of $arr and $q.

Done. @Script47

@koeneman but it says "Notice" and not "Error"...?

1 Answer


Assuming, your input woud be ?q=lastname.kate you are going to run substr with a length of 4 (kate) on a string with length 3 (rob) within the first foreach-cycle. This will cause an undefined offset error.

posted this

Have an answer?


Please login first before posting an answer.