PHP如果语句没有显示正确的结果

时间:2014-02-04 09:02:09

标签: php

我有这些PHP if语句,当我回显$percentage时它显示100但它的回显

echo 'Over Allowance';
echo '<br>INSERT into customer_billing';

而不是

echo 'Reached Allowance';

if($percentage >= '80' and $percentage <= '100') {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Approaching Allowance';
            echo 'check customer_billing and remove';
        }
        //if its 100%
        elseif($percentage == '100') {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Reached Allowance';
            echo 'check customer_billing and remove';
        }
        //if its more than 100%
        elseif($percentage > '100') {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Over Allowance';
            echo '<br>INSERT into customer_billing';
        }
        //or if the limit is less than the percentage
        //this does nothing at all
        elseif($percentage < $result["soundfiles_max"]) {
            //do nothing
            echo 'check customer_billing and remove';
        }

我做错了什么?

4 个答案:

答案 0 :(得分:2)

你可以删除第一个陈述中的相等,

if($percentage >= '80' && $percentage < '100') {

答案 1 :(得分:0)

由于你使用的是&gt; =运算符,你应该使用整数而不是文本,并删除那些数字上的'',所以从'80'到80只

if($percentage >= 80 && $percentage <= 100) {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Approaching Allowance';
            echo 'check customer_billing and remove';
        }
        //if its 100%
        elseif($percentage == 100) {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Reached Allowance';
            echo 'check customer_billing and remove';
        }
        //if its more than 100%
        elseif($percentage > 100) {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Over Allowance';
            echo '<br>INSERT into customer_billing';
        }
        //or if the limit is less than the percentage
        //this does nothing at all
        elseif($percentage < $result["soundfiles_max"]) {
            //do nothing
            echo 'check customer_billing and remove';
        }

答案 2 :(得分:0)

尝试删除数字值周围的引号,例如:

if($percentage >= '80' and $percentage <= '100') {

变为:

if($percentage >= 80 and $percentage <= 100) {

答案 3 :(得分:0)

忽略百分比值的引号,因为它们是整数。像

if($percentage >= 80 and $percentage <= 100) 

如果$百分比值为100,则不会转到elseif语句,因为您在第一个语句中检查了大于等于100。

参考floating point numbers

  

永远不会将浮点数结果信任到最后一位数,而不是   直接比较浮点数是否相等。如果更高   精度是必要的,任意精度数学函数和gmp   功能可用。