Search code examples
phpnotice

Notice: unintialized string offset


I have the following code below, which workes quite allright - but now when I have all errors reported on - I get to see this:

Notice: Uninitialized string offset: 9 in C:\xampp\htdocs\website\dev\lib\player.class.php on line 110 Notice: Uninitialized string offset: 10 in C:\xampp\htdocs\website\dev\lib\player.class.php on line 110 Notice: Uninitialized string offset: 11 in C:\xampp\htdocs\website\dev\lib\player.class.php on line 110 Notice: Uninitialized string offset: 12 in C:\xampp\htdocs\website\dev\lib\player.class.php on line 110 Notice: Uninitialized string offset: 13 in C:\xampp\htdocs\website\dev\lib\player.class.php on line 110

Line 110 is

$this->formattedname .= "<span style='color:" . $colour . "'>" . $this->username[$i] . "</span>";

in the foreach

Does anyone know what i'm doing wrong? I can't get a solution to fix these errors.. :(

if ($this->admin == 1) {
            $colours = explode("~", $this->gradientcolours);
            $gradient = new ColourGradient(array(0 => $colours['0'], (strlen($this->username) - 1) => $colours['1']));
            $this->formattedname .= ($this->admin == 1) ? "<b><i><a style='text-decoration: none;' title='" . $this->title . "' href='/profile/" . $this->id . "'>" : "<b><a title='" . $this->title . "' href='/profile/" . $this->id . "'>";
            $this->formattedname2 = ($this->admin == 1) ? "<b><i><a style='text-decoration: none;' title='" . $this->title . "' href='/profile/" . $this->id . "'>" : "<b><a title='" . $this->title . "' href='/profile/" . $this->id . "'>";



                    foreach($gradient as $i => $colour) {
                $this->formattedname .= "<span style='color:" . $colour . "'>" . $this->username[$i] . "</span>";

            }

            $this->formattedname .= ($this->admin == 1) ? "</a></i></b>" : "</a></b>";

            $this->formattedname2 .= ($this->admin == 1) ? "</a></i></b>" : "</a></b>";

        }

Solution

  • This error would occur if any of the following variables were actually strings or null instead of arrays.

    Try to test and initialize your arrays before you use them

    if( !isset($this->username[$i]) ) $this->username[$i] = '' ;