-2

What is Undefined offset error. I am new in PHP world. I write some code.But error appear like that.

Notice: Undefined offset: 5 in D:\xammp\htdocs\dashboard\ali.php on line 15 Student 5:

Notice: Undefined offset: 6 in D:\xammp\htdocs\dashboard\ali.php on line 15 Student 6

here is code.

<?php

    $ali = array();
    $ali[1] = 1.3;
    $ali[2] = 3.2;
    $ali[3] = 3.4;
    $ali[4] = 4.3;

    for($i = 7; $i <= 150; $i++){
        $ali[$i] = mt_rand(20, 40) * 0.1; 
    }

    $x = 1;
    while($x < 7){
        echo "Student $x: " . $ali[$x] . "<br />";
        $x +=1;
    }

?>
ali naeem
  • 1
  • 1
  • Would be good to also explain what you are trying to achieve. You might get better solutions for your problem – urban Dec 27 '15 at 13:35

1 Answers1

0

Yes, you are setting from 1 till 4, then skipping 5 and 6, then starting with 7:

$ali = array();
$ali[1] = 1.3;
$ali[2] = 3.2;
$ali[3] = 3.4;
$ali[4] = 4.3;
// 5 and 6 not defined, but started with 7 assignment for the next loop.

So add values for 5 and 6, or change it:

for($i = 5; $i <= 150; $i++){
    $ali[$i] = mt_rand(20, 40) * 0.1; 
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252