Notice: Use of undefined constant for already defined variable [duplicate]

335
June 02, 2017, at 6:37 PM

This question already has an answer here:

  • Reference - What does this error mean in PHP? 29 answers

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?

Answer 1

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]);
        }
READ ALSO
Zend framework 3 custom elements

Zend framework 3 custom elements

We are used to work with ZF2, but for our last project, we decided to start with ZF3Now I am facing a problem in the form creation

330
laravel ssh2 exec command

laravel ssh2 exec command

I'm trying to create something based on ssh2 connecting to my vps and then creating some files and running them with exec commands

410
mysql select table drop list order by inn group [on hold]

mysql select table drop list order by inn group [on hold]

i want to group by and in group show order by list what can i do ??

282
track Email status mailgun php

track Email status mailgun php

I want to check the email status in mailgun api,

268