我有这个代码。
$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 - 在第 40 行的 /Applications/XAMPP/xamppfiles/htdocs/learningphp/myfirstfile.php 中假定为“rollcounts”
显然,$rollcounts
已经在代码中定义了。那么这里的问题是什么?
你忘记了$
旧代码
if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}
新代码
if($rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句