这段代码:
$hour = 2.2;
if (floor($hour) > 1) {
$str = $str . floor($hour) . " hours";
}
else if (floor($hour) === 1) {
$str = $str . floor($hour) . " hour";
}
echo $str;
将输出:2 hours
然而,这一点:
$hour = 1.2;
if (floor($hour) > 1) {
$str = $str . floor($hour) . " hours, ";
}
else if (floor($hour) === 1) {
$str = $str . floor($hour) . " hour ";
}
echo $str;
不输出1 hour
,因为else if
的条件由于某种原因不匹配。那是为什么?
答案 0 :(得分:3)
Floor返回一个浮点数,你正在测试一个整数。
http://php.net/manual/en/function.floor.php
为清晰起见编辑:
<?php
$str = "";
$hour = 1.2;
if (floor($hour) > 1) {
$str = $str . floor($hour) . " hours, ";
}
else if ((int) floor($hour) === 1) {
$str = $str . floor($hour) . " hour ";
}
echo $str;
?>