如何安排输出评级(good、great、exxelent等)的php if语句?

如何安排输出评级(good、great、exxelent等)的php if语句?,php,css,if-statement,Php,Css,If Statement,我使用php if语句向与评级相关的div添加类: CSS: .average .rating-result { color: #888; /* 0~1 votes */ } .good .rating-result { color: #9a6e65; /* 2~3 votes */ } .great .rating-result { color: #aa5443; /* 4~5 votes */ } .excellent .rating-result { col

我使用php if语句向与评级相关的div添加类:

CSS:

.average .rating-result {
    color: #888; /* 0~1 votes */
}
.good .rating-result {
    color: #9a6e65; /* 2~3 votes */
}
.great .rating-result {
    color: #aa5443; /* 4~5 votes */
}
.excellent .rating-result {
    color: #bb3b22; /* 6~7 votes */
}
.brilliant .rating-result {
    color: #cc2200; /* 8~9 votes */
}
 <div class="topic-like-count<?php if ( $thumbs_number == 0 || 1 ) {
 echo " good"; } elseif ( $thumbs_number == 2 || 3 ) { echo " great"; }
 elseif ( $thumbs_number == 4 || 5 ) { echo " excellent"; } elseif (
 $thumbs_number > 6 || 7 ) { echo " brilliant"; } else { echo "
 average"; }?>"> h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
 </div>
PHP:

.average .rating-result {
    color: #888; /* 0~1 votes */
}
.good .rating-result {
    color: #9a6e65; /* 2~3 votes */
}
.great .rating-result {
    color: #aa5443; /* 4~5 votes */
}
.excellent .rating-result {
    color: #bb3b22; /* 6~7 votes */
}
.brilliant .rating-result {
    color: #cc2200; /* 8~9 votes */
}
 <div class="topic-like-count<?php if ( $thumbs_number == 0 || 1 ) {
 echo " good"; } elseif ( $thumbs_number == 2 || 3 ) { echo " great"; }
 elseif ( $thumbs_number == 4 || 5 ) { echo " excellent"; } elseif (
 $thumbs_number > 6 || 7 ) { echo " brilliant"; } else { echo "
 average"; }?>"> h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
 </div>

这将使其始终显示良好

|| 1 

如果语法错误:
如果($thumbs_number==0 | | 1)
计算结果为:
如果($thumbs_number==0)| | 1)
这总是正确的


您应该这样写:
如果($thumbs\u number==0 | |$thumbs\u number==1)
将其合并到开关状态中会更有意义

switch ( $thumbs_number ) {
    case 0:
    case 1:
        $rating = 'good';
        break;
    case 2:
    case 3:
        $rating = 'great'; 
        break;
    // etc ...
}
?>

<div class="topic-like-count <?=$rating?>">
开关($thumbs\u编号){
案例0:
案例1:
$评级='良好';
打破
案例2:
案例3:
$评级='很棒';
打破
//等等。。。
}
?>

对我肯定会在这里使用案例陈述。事实上,我可能只会使用一个数据库查找表,当我首先检索文本时,它会根据评分返回文本,但这是另一个故事。。。我可能还想把评级分配给HTML输出之外的一个变量,从而将逻辑与显示分开一点。@Matt Gibson我是个初学者。我已经很难过了。我以后可能会试试你的建议。@pritae最后一个问题,我如何在switch语句中添加“或多”或“少”?@alexchenco没问题。我认为每个人首先都必须这样做;如果你从一开始就试图“正确地”做这件事,你会被新概念淹没,并被推迟。@alexchenco你说的“或更多”是什么意思?在这个switch语句中,0或1都将生成“good”,例如,如果您不清楚发生了什么,则使用a。
switch ( $thumbs_number ) {
    case 0:
    case 1:
        $rating = 'good';
        break;
    case 2:
    case 3:
        $rating = 'great'; 
        break;
    // etc ...
}
?>

<div class="topic-like-count <?=$rating?>">