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

时间:2015-09-15 21:41:22

标签: php if-statement

我写了一个评估歌曲评级的简单代码。我们试图根据返回的百分比显示星星。

问题在于我们有多个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语句来确定其他东西并且它工作正常。但是这个没有。

感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

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"

答案 1 :(得分:3)

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

接下来,这段代码简直太丑了,你在这里重复很多事情。尝试重构它。

答案 2 :(得分:1)

我测试了将$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将始终在&lt; = 0,1-4星形比较和&gt; 80之间的范围内