$answers = array("a", "b", "c", "a", "a", "a");
$S1Q1 = $_POST['S1Q1'];
$S1Q2 = $_POST['S1Q2'];
$S2Q1 = $_POST['S2Q1'];
$S2Q2 = $_POST['S2Q2'];
$S2Q3 = $_POST['S2Q3'];
$S2Q4 = $_POST['S2Q4'];
$totalValue .= "About OM | Question 01: " . $S1Q1 . " " . ($S1Q1 == $correct[0]) ? 'Correct' : 'Wrong' . "\n";
$totalValue .= " | Question 02: " . $S1Q2 . " " . ($S1Q2 == $correct[1]) ? 'Correct' : 'Wrong' . "\n\n";
$totalValue .= "About EHS | Question 01: " . $S2Q1 . " " . ($S2Q1 == $answers[2]) ? 'Correct' : 'Wrong' . "\n";
$totalValue .= " | Question 02: " . $S2Q2 . " " . ($S2Q2 == $answers[3]) ? 'Correct' : 'Wrong' . "\n";
$totalValue .= " | Question 03: " . $S2Q3 . " " . ($S2Q2 == $answers[4]) ? 'Correct' : 'Wrong' . "\n";
$totalValue .= " | Question 04: " . $S2Q4 . " " . ($S2Q2 == $answers[5]) ? 'Correct' : 'Wrong' . "\n\n";
我有上面的php脚本,我可以用什么方法或函数来计算用户在一个循环中有多少正确和多少错误,然后计算百分比?
示例:
正确:4
错误:2
总计:6
正确百分比:((4/6) * 100) = 66.666666666% => 将其四舍五入为66%