0

I've got this code.

    $rollcount=0;
    $rollcounts=array(); //I define $rollcounts here
    $number_of_tries = 100;

    foreach(range(0,$number_of_tries-1) as $i){
        do{ 
        $roll=rand(1,6);
        $rollcount++;
    }while($roll!=6);
    array_push($rollcounts, $rollcount);
    $rollcount = 0;
    }

    $freqs = array();
    while (!empty($rollcounts)){

        $freq = count(array_filter($rollcounts,function($a) use ($rollcounts)
            {return $a == $rollcounts[0];}
        ));
        $freqs[$rollcounts[0]] = $freq; 
        for($i=0;$i<count($rollcounts);$i++){ 

            if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
                unset($rollcounts[$i]);
            }

        }

    } // redo until $rollcounts is empty

That generates this error message (line 40 has been commented in the code)

Notice: Use of undefined constant rollcounts - assumed 'rollcounts' in /Applications/XAMPP/xamppfiles/htdocs/learningphp/myfirstfile.php on line 40

Clearly, $rollcounts has already been defined in the code. So what is the problem here?

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Sahand
  • 7,980
  • 23
  • 69
  • 137
  • 1
    Missing a variable identifier `$`, so php thinks it's a constant. Look at the error, it's an undefined *constant*, not undefined variable. – Qirel Jun 02 '17 at 09:15

1 Answers1

2

You forgot a $

Old code

        if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
            unset($rollcounts[$i]);
        }

New code

        if($rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
            unset($rollcounts[$i]);
        }
Remco K.
  • 644
  • 4
  • 19