This is the switch statement. Only if the variable $thumbs_number
is less than -1 (eg -2, -3, -4, etc.), the class bad
should output.
But right now, the class bad
is also being output when $thumbs_number
is 0 (-1 and 1 have the right class: average
).
<div class="topic-like-count
<?php // Apply style based on number of votes
switch ($thumbs_number) {
case ($thumbs_number < -1): echo ' bad'; break;
case ($thumbs_number == 0):
case ($thumbs_number == 1): echo ' average'; break;
case ($thumbs_number == 2):
case ($thumbs_number == 3): echo ' good'; break;
case ($thumbs_number == 4):
case ($thumbs_number == 5): echo ' great'; break;
case ($thumbs_number == 6):
case ($thumbs_number == 7): echo ' excellent'; break;
case ($thumbs_number > 7): echo ' brilliant'; break;
}
?>
">
What is happening?
You a misusing the switch
statement.
Each case
statement compares the result of the expression to the value passed to switch
. So here you are comparing $thumbs_number
to the result of ($thumbs_number < -1)
, which is either true
or false
.
Do this instead:
switch ($thumbs_number) {
case 0:
case 1:
echo "average";
break;
case 2:
case 3:
echo "good";
break;
....
default:
if ($thumbs_number <= -1) echo "bad";
else if ($thumbs_number > 7) echo "brillant";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.