多个IF语句只返回最后一个值- PHP


Multiple IF Statements only returning last value - PHP

我编写了一个简单的代码来评估歌曲评分。我们正在尝试根据返回的百分比显示星星。

问题是我们有多个IF语句来评估百分比并返回歌曲的正确星星数量。无论歌曲的评分是多少,IF语句总是返回5颗星,而不是返回正确的星数。

我们正在使用一个标志来确定是否在任何if语句中找到匹配。

function displayRatingStars() {
    /* Some Other Code */
    $likes = $json_data['items'][0]['statistics']['likeCount'];
    $dislikes = $json_data['items'][0]['statistics']['dislikeCount'];
    $found = false; //flag to determine if match was found in any of the if statements
    $rating_total = $likes + $dislikes;
    $rating_percentage = number_format(($likes / $rating_total) * 100);
    /* no stars */  if ($rating_percentage <= 0) { echo '<div class="text-info padder m-t-sm text-sm"><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i></div>'; $found = true; }
    /* 1 star */    else if ($rating_percentage > 0 && $rating_percentage < 20) { echo '<div class="text-info padder m-t-sm text-sm"><i class="fa fa-star"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i></div>'; $found = true; }
    /* 2 stars */   else if ($rating_percentage > 20 && $rating_percentage < 40) { echo '<div class="text-info padder m-t-sm text-sm"><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i></div>'; $found = true; }
    /* 3 stars */   else if ($rating_percentage > 40 && $rating_percentage < 60) { echo '<div class="text-info padder m-t-sm text-sm"><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i></div>'; $found = true; }
    /* 4 stars */   else if ($rating_percentage > 60 && $rating_percentage < 80) { echo '<div class="text-info padder m-t-sm text-sm"><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o text-muted"></i></div>'; $found = true; }
    /* 5 stars */   else if ($rating_percentage > 80) { echo '<div class="text-info padder m-t-sm text-sm"><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i></div>'; $found = true; }
    /* no match */  else { echo '<div class="text-info padder m-t-sm text-sm"><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i><i class="fa fa-star-o text-muted"></i></div>'; $found = true; }
}

是否有一个原因,为什么这个代码直接跳转到5星,而不是找到正确的匹配和忽略其他的?

我有一个非常类似的函数,使用IF语句来确定其他东西,它工作得很好。但是这个没有。

Fencepost错误:

if ($x < 20) { ... }
else if ($x > 20) { ... }

如果$x恰好是20呢?你违反了else条款。

你的比较链应该是:

($rating_percentage > 0 && $rating_percentage <= 20)
($rating_percentage > 20 && $rating_percentage <= 40)
etc..

注意<=

加,number_format()返回STRING。该字符串旨在以人性化的格式显示数字。你没有在一个对人类友好的环境中使用它。你取那个格式化的字符串然后把它进行一系列整数比较。

虽然在您的特殊情况下这无关紧要,但请考虑如下:

$x = 1000;
$y = number_format($x); // "1,000"
($x < 999) -> FALSE
($y < 999) -> TRUE, because 1,000 in numeric context becomes "1"

number_format()函数将返回字符串,因此您不应该将其与整数进行比较。这是第一个问题。

接下来,这段代码很难看,你在这里重复了很多东西。试着重构它

我测试了将$rating_percentage设置为不同的值并且它有效。您遇到的问题首先是number_format()功能。

参见http://php.net/manual/en/function.number-format.php -该函数返回一个字符串,不能与ifelse if语句中的数字进行比较。

round(($likes / $rating_total) * 100, 0)函数代替number_format()函数

还要注意Marc B的回答,你在价值链上有差距。此外,如果这个问题被修复,最终的else将永远不会实现。($rating_percentage将始终在<= 0,1 -4星比较和> 80之间)