u0867587 Posted November 4, 2011 Share Posted November 4, 2011 My case statement is not working. What happened is that two students got a $markgrade of 61 and 67. So they both should get grade B but instead they both get Grade A. 1 student got 55 which should be grade C but gets grade A. why is it not following the switch statement? function outputModule($moduleID, $moduleName, $sessionData) { if(!count($sessionData)) { return false; } $markTotal = 0; $markGrade = 0; $weightSession = 0; $grade = ""; $sessionsHTML = ''; switch($grade){ case ($markGrade >=70): $grade = 'A'; break; case ($markGrade >=60 && $markGrade <=69): $grade = 'B'; break; case ($markGrade >=50 && $markGrade <=59): $grade = 'C'; break; case ($markGrade >=40 && $markGrade <=49): $grade = 'D'; break; case ($markGrade >=30 && $markGrade <=39): $grade = 'E'; break; case ($markGrade >=0 && $markGrade <=29): $grade = 'F'; break; }; foreach($sessionData as $session) { $sessionsHTML .= "<p><strong>Session:</strong> {$session['SessionId']} {$session['Mark']} {$session['SessionWeight']}%</p>\n"; $markTotal += ($session['Mark'] / 100 * $session['SessionWeight']); $weightSession += ($session['SessionWeight']); $markGrade = ($markTotal / $weightSession * 100); } $moduleHTML = "<p><br><strong>Module:</strong> {$moduleID} - {$moduleName} {$markTotal} {$markGrade} {$grade}</p>\n"; return $moduleHTML . $sessionsHTML; } Quote Link to comment https://forums.phpfreaks.com/topic/250427-my-case-statement-is-not-working/ Share on other sites More sharing options...
KevinM1 Posted November 4, 2011 Share Posted November 4, 2011 You're switching on $grade, yet trying to make cases for $markGrade. Further, cases cannot be used as normal conditionals. You can only use them when you anticipate that your variable will equal a certain value, such as: switch($i) { case 0: echo $i; break; case 1: echo "\$i = 1"; break; } switch So, just use normal if/elseif/else clauses. Quote Link to comment https://forums.phpfreaks.com/topic/250427-my-case-statement-is-not-working/#findComment-1284871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.