计数器初始化(可以在每次步进前执行):
if(!isset($_SESSION['correctAnswers'])
$_SESSION['correctAnswers'] = 0;
if(!isset($_SESSION['wrongAnswers'])
$_SESSION['wrongAnswers'] = 0;
计数器步进:
if(...) {
$_SESSION['result'] = 'Correct Answer!';
$_SESSION['correctAnswers'] += 1;
}else{
$_SESSION['result'] = 'Wrong Answer!';
$_SESSION['wrongAnswers'] += 1;
}
分数:
$correctAnswers = $_SESSION['correctAnswers'];
$totalAnswers = $_SESSION['wrongAnswers'] + $correctAnswers;
if($totalAnswers > 0)
$score = $correctAnswers / ($totalAnswers) * 100;
else
$score = 0;