PHP ELSE IF 语句
无法让我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 round 的第三个参数,您可以将其向下舍入
并用于您的
if
你的else if
条件永远不会成立,就好像$waveFeet
大于或等于第一个条件2
将为真,因此您的elseif
条件永远不会 真的。你应该将其更改为
Using the third argument of round you can round it down
and for your
if
yourelse if
condition will never got true as if the$waveFeet
is greater than or equal to2
, the first condition will be true hence yourelseif
condition will never be true.You should be changing it to
尝试使用特定值的语句,例如
$waveFeet = 10;
,然后单步执行代码。第一个条件成功,因此后面的分支永远不会被检查。事实上,唯一没有进入第一个分支的情况是当 $waveFeet$waveFeet
$waveFeet
$waveFeet
$waveFeet
$waveFeet
$waveFeet < 2
,在这种情况下,将执行最后一个分支主体。因此中间分支永远不会被执行。应该首先考虑更排他的情况:为了完全安全,您可以指定两个边界条件:
由于冗余而导致的低效率最小,并且代码更清晰。当你变得更有经验时,你就可以放弃这种事情。
如果您希望向下舍入偶数负数,请使用
floor
。如果您希望向零舍入(即截断),请转换为 int: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:To be completely safe, you can specify both boundary conditions:
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:你可以使用地板进行下一轮
你必须首先检查 5 bcz 5 是否大于 2 并且你的第一个条件 >= 2 如果没有 > 5 也满足,所以控制转到第一个条件而不是第二个....
you can use floor for down round
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....
您的代码中没有任何问题(除了您可以将参数传递给不带引号的函数)。你检查的方式是错误的。
它不会转到 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 .PHP 内置的
floor()
和ceil()
函数分别向下和向上舍入数字。我建议发布您收到的错误,以便我们可以更快地帮助您 =)试试这个:
注意第一个条件的更改(添加 && $waveFeet <= 5)
我认为问题可能是您在第一个和第二个条件中使用的范围是重叠的,并且很可能在这种情况下,
$waveFeet == 6
PHP 评估您的第一个条件(最初是$waveFeet >= 2
),并且它恰好是true
,因此 PHP 不会测试else if
语句...只要可以使用析取条件,我建议你这样做...PHP buil-in
floor()
andceil()
functions round a number down and up respectively. I recommend posting the error you get so we can help you faster =)Try this:
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 betrue
, so PHP does not test theelse if
statement... Whenever it's possible to use disjunct conditions, I recommend you to do it...