Search code examples
phparraysstringnotice

Everything works but i have : Notice: Uninitialized string offset,


I need your help because my code works, it do what i exepcted but i have this : Notice: Uninitialized string offset. But i have no idea of how change that to make the notice disappear. If someone can help me, i would be grateful ! Have a great day everyone.

There is my code :

$bool = false;
    $chaine = $_POST['search'];
    for($i=0; $i<= strlen($chaine); $i++){
        if($chaine[$i] == " "){
            $bool = true;
        }
    }

And i got this on my page :

Notice: Uninitialized string offset: 8 in /navbar.php on line 47

But it works fine.

Thanks for theses quick answer ! It's works fine whithout the notice now, i didn't checked the value of strlen who it started at 1, that's why.


Solution

  • You will need to replace <= with < so that it doesn't exceed the length:

    $bool = false;
        $chaine = $_POST['search'];
        for($i=0; $i< strlen($chaine); $i++){
            if($chaine[$i] == " "){
                $bool = true;
            }
        }