You are trying to compare $point with a boolean (e.g. $point >= 80 ). This corresponds to the first case, because $point = 0 is false, and $point >= 80 also false, so it corresponds to the first case and the echo A If you want to use case-by-case comparison, you should use the following code:
$point = 0; switch (true) { case $point >= 80: echo 'A'; break; case $point >= 70: echo 'B'; break; case $point >= 50: echo 'C'; break; case $point >= 30: echo 'D'; break; case $point >= 0: echo 'E'; break; default: echo 'F'; break; }
demo: https://ideone.com/lyBUmA
Another solution using if and elseif instead of switch :
$point = 0; if ($point >= 80) { echo 'A'; } elseif ($point >= 70) { echo 'B'; } elseif ($point >= 50) { echo 'C'; } elseif ($point >= 30) { echo 'D'; } elseif ($point >= 0) { echo 'E'; } else { echo 'F'; }
demo: https://ideone.com/FRlorS
source share