我有这段代码。
$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
生成此错误消息(第40行已在代码中注释)
注意:使用未定义的常量rollcounts - 假设'rollcounts' 在/Applications/XAMPP/xamppfiles/htdocs/learningphp/myfirstfile.php中 在第40行
显然,代码中已经定义了$rollcounts
。那么这里的问题是什么?
答案 0 :(得分:2)
您忘了 $
旧代码
if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}
新代码
if($rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}