2017-06-02 110 views
-1

我有這段代碼。注意:對已定義的變量使用未定義的常量

$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' /應用程序/ XAMPP/xamppfiles/htdocs中第40行

顯然/learningphp/myfirstfile.php ,$rollcounts已經在代碼中定義。那麼這裏有什麼問題?

+0

缺少一個變量標識符'$',所以PHP認爲這是一個常數。看看錯誤,這是一個未定義的*常量*,而不是未定義的變量。 – Qirel

回答

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]); 
     } 
+0

乾杯的人。菜鳥失明。 – Sahand