1 回答

TA貢獻1847條經驗 獲得超7個贊
$wrong = 0;在前循環中!因此,在每次迭代之后,它再次使其為0。
您的代碼應如下所示:
$wrong = 0;
foreach($all_files as $file) {
$each_file = file_get_contents($file);
$choosen_answer = substr($each_file, -1); // last number is the number user has choosen
$correct_answer = substr($each_file, 0,1); // first number is the correct answer
// output the answers
echo '<span class="choosen_answer">'.$choosen_answer.'</span>';
echo '<span class="correct_answer">'.$correct_answer.'</span>';
if($choosen_answer != $correct_answer) { // compare the 2 values
$wrong++;
echo '<span class="wrong text-danger">Wrong</span>';
}
else {
echo '<span class="correct text-success">Correct</span>';
}
}
echo $wrong;
- 1 回答
- 0 關注
- 110 瀏覽
添加回答
舉報