## 带有比较运算符和小数位的PHP脚本逻辑内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (11)

``````for(\$x=1;\$x<=\$this->retreat->total_review_activities_rating;\$x++) {
echo '<i class="fas fa-star"></i>';
}
if (strpos(\$this->retreat->total_review_activities_rating,'.')) {
echo '<i class="fas fa-star-half-alt"></i>';
\$x++;
}
while (\$x<=5) {
echo '<i class="far fa-star"></i>';
\$x++;
}
``````

### 2 个回答

`strpos()`函数将始终输出true，因为它输出一个位置，在布尔逻辑下不是0（false）。

``````while(\$x > 0) { //While there is still enough rating for a star left
if(\$x >= 0.75) {
echo '<i class="fas fa-star"></i>';
\$x--; //Remove one start rating
else if(\$x < 0.25) {
//Don't display a start
\$x--; //There isn't enough left for a star so we must be done.
else {
//Display a half-star
echo '<i class="fas fa-star-half-alt"></i>';
\$x--; //There is only enough for a half start left so we must be done
}
}
``````

``````if (\$this->retreat->total_review_activities_rating != (int)\$this->retreat->total_review_activities_rating) {
// if (4.800 != 4) {true,  show half star}
// if (5.000 != 5) {false, no half star}
echo '<i class="fas fa-star-half-alt"></i>';
\$x++;
}
``````