Question

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>";

        }
Was it helpful?

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] = '' ;

OTHER TIPS

That is not an error, but a notice. An error or a warning imply you did something wrong/made an mistake. A notice is just a hint that something might be done better.

What is is telling you, is that you are using a key which doesnt exist:

$array[0] = 0;
$array[1] = 1;
$array[2] = 2;

echo $array[0]; // no notice, it exists
echo $array[2]; // no notice, it exists
echo $array[9]; // the notice will fire, because key 9 doesn't exist

In your code I cant tell which line is 110, but im guessing $this->username[$i] is the problem, user 9 to 13 dont exist

If $this->username is not an array but a string, it will give back the Nth character:

$string = "example";
echo $string[1]; // will return X, an array starts counting at 0 (zero-index-based)
echo $string[50]; // blank echo, and the notice because character 50 doesn't exist

If you use concatenation .= for a string, Your string must be initialized like this

$this->formattedname = "";

and after concatenate

The issue is with this line:

$this->username[$i].

Your username may have say 5 characters, while $i can be say 12. Use proper conditions to validate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top