PHP ELSE IF 语句

发布于 2024-12-19 09:55:09 字数 405 浏览 2 评论 0原文

无法让我的 else if 语句发挥作用,有人有什么想法吗?它可以在没有 else if 的情况下工作....

$waveFeet = round("$ar2");
        if ($waveFeet >= 2) {
            echo $waveFeet - 1;
        }
        else if ($waveFeet > 5) {
            echo $waveFeet - 2;
        }

        else 
        {
            echo "$wavefeet";   
        }

也作为一个附带问题,任何人都可以告诉我如何更改我的 round() 以使其始终舍入(向下)而不是向上或向下舍入...?

unable to get my else if statement to work, does anyone have any ideas? it works without the else if....

$waveFeet = round("$ar2");
        if ($waveFeet >= 2) {
            echo $waveFeet - 1;
        }
        else if ($waveFeet > 5) {
            echo $waveFeet - 2;
        }

        else 
        {
            echo "$wavefeet";   
        }

also as a side question, can anyone tell me how to change my round() to make it always round (down) instead of rounding up or down...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

×眷恋的温暖 2024-12-26 09:55:09

使用 round 的第三个参数,您可以将其向下舍入

echo $waveFeet = round($ar2, 2,  PHP_ROUND_HALF_DOWN);

并用于您的 if 你的 else if 条件永远不会成立,就好像 $waveFeet 大于或等于第一个条件 2将为真,因此您的 elseif 条件永远不会 真的。

你应该将其更改为

    if ($waveFeet > 5) {
        echo $waveFeet - 1;
    }
    else if ($waveFeet >= 2) {
        echo $waveFeet - 2;
    }    
    else 
    {
        echo $wavefeet;   
    }

Using the third argument of round you can round it down

echo $waveFeet = round($ar2, 2,  PHP_ROUND_HALF_DOWN);

and for your if your else if condition will never got true as if the $waveFeet is greater than or equal to 2, the first condition will be true hence your elseif condition will never be true.

You should be changing it to

    if ($waveFeet > 5) {
        echo $waveFeet - 1;
    }
    else if ($waveFeet >= 2) {
        echo $waveFeet - 2;
    }    
    else 
    {
        echo $wavefeet;   
    }
耳钉梦 2024-12-26 09:55:09

尝试使用特定值的语句,例如 $waveFeet = 10;,然后单步执行代码。第一个条件成功,因此后面的分支永远不会被检查。事实上,唯一没有进入第一个分支的情况是当 $waveFeet $waveFeet $waveFeet $waveFeet $waveFeet $waveFeet $waveFeet < 2,在这种情况下,将执行最后一个分支主体。因此中间分支永远不会被执行。应该首先考虑更排他的情况:

if (5 < $waveFeet) {
    ...
} elseif (2 <= $waveFeet) {
    ...
} else {
    # $waveFeet < 2
    ...
}

为了完全安全,您可以指定两个边界条件:

    ...
} elseif (2 <= $waveFeet && $waveFeet <= 5) {
    ...

由于冗余而导致的低效率最小,并且代码更清晰。当你变得更有经验时,你就可以放弃这种事情。

如果您希望向下舍入偶数负数,请使用floor。如果您希望向零舍入(即截断),请转换为 int:

$waveFeet = (int) $ar2;

Try the statement with a particular value, say $waveFeet = 10;, then step through the code. The first condition succeeds, so the later branches are never checked. In fact, the only time the first branch isn't entered is when $waveFeet < 2, in which case the last branch body will be executed. Thus the middle branch is never executed. The more exclusive case should come first:

if (5 < $waveFeet) {
    ...
} elseif (2 <= $waveFeet) {
    ...
} else {
    # $waveFeet < 2
    ...
}

To be completely safe, you can specify both boundary conditions:

    ...
} elseif (2 <= $waveFeet && $waveFeet <= 5) {
    ...

The inefficiency due to redundancy is minimal and the code is clearer. As you get more experienced, you can leave off this sort of thing.

If you wish to round even negative numbers down, use floor. If you wish to round towards zero (i.e. truncate), cast to an int:

$waveFeet = (int) $ar2;
这个俗人 2024-12-26 09:55:09

你可以使用地板进行下一轮

$waveFeet = floor($ar2);                

if ($waveFeet > 5)
    echo $waveFeet - 2;
else if ($waveFeet >= 2) 
    echo $waveFeet - 1;
else 
    echo $wavefeet;   

你必须首先检查 5 bcz 5 是否大于 2 并且你的第一个条件 >= 2 如果没有 > 5 也满足,所以控制转到第一个条件而不是第二个....

you can use floor for down round

$waveFeet = floor($ar2);                

if ($waveFeet > 5)
    echo $waveFeet - 2;
else if ($waveFeet >= 2) 
    echo $waveFeet - 1;
else 
    echo $wavefeet;   

you have to first check for 5 bcz 5 is big no than 2 and your first condition >= 2 also satisfied if no >5 so control go to first condition rather than second....

春夜浅 2024-12-26 09:55:09

您的代码中没有任何问题(除了您可以将参数传递给不带引号的函数)。你检查的方式是错误的。

它不会转到 else if 条件,因为条件将在第一次检查本身中得到满足。

Theres nothing wrong in your code ( except that you can pass argument to function without quotes ). The way you are checking it is wrong.

it wont go to else if condition because the condition will be satisfied in the first check itself .

菊凝晚露 2024-12-26 09:55:09

PHP 内置的 floor()ceil() 函数分别向下和向上舍入数字。我建议发布您收到的错误,以便我们可以更快地帮助您 =)

试试这个:

$waveFeet = floor($ar2);
if ($waveFeet >= 2 && $waveFeet <= 5 ) {
    echo $waveFeet - 1;
} else if ($waveFeet > 5) {
    echo $waveFeet - 2;
} else {
    echo $wavefeet;   
}

注意第一个条件的更改(添加 && $waveFeet <= 5

我认为问题可能是您在第一个和第二个条件中使用的范围是重叠的,并且很可能在这种情况下,$waveFeet == 6 PHP 评估您的第一个条件(最初是 $waveFeet >= 2),并且它恰好是 true,因此 PHP 不会测试 else if 语句...只要可以使用析取条件,我建议你这样做...

PHP buil-in floor() and ceil() functions round a number down and up respectively. I recommend posting the error you get so we can help you faster =)

Try this:

$waveFeet = floor($ar2);
if ($waveFeet >= 2 && $waveFeet <= 5 ) {
    echo $waveFeet - 1;
} else if ($waveFeet > 5) {
    echo $waveFeet - 2;
} else {
    echo $wavefeet;   
}

Note the change in the first condition (added && $waveFeet <= 5)

I think the problem might be that the ranges you use in your first and second conditions are overlapped, and it is very likely that in the case, let's say, $waveFeet == 6 PHP evaluates your first condition (originally $waveFeet >= 2), and it happened to be true, so PHP does not test the else if statement... Whenever it's possible to use disjunct conditions, I recommend you to do it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文