floor() 函数的作用是将输入值向下舍入,得到最接近的整数结果。
语法
floor(x)
| 参数 | 描述 |
|---|---|
| x | 必需。一个数。 |
说明
该函数返回不大于参数 x 的最大整数,直接舍弃 x 的小数部分。需要注意的是,floor() 的返回值类型依然是 float,这是因为 float 类型的数值范围通常比 integer 类型更大。
以下针对 PHP 中的八种主要数据类型作为 floor() 参数时的表现进行分析。
<?php
/* boolean */
echo floor(false);//输出 0
echo floor(true);//输出 1</code></pre>
<pre class="brush:php;"><code class="language-php">
/*float or integer */
echo floor(0.60); //输出 0
echo floor(5); //输出 5
echo floor(-5.9)//输出 -6</code></pre>
<pre class="brush:php;"><code class="language-php">
/* string */
echo floor('0.60'); //输出 0
echo floor('5'); //输出 5
echo floor('-5.9')//输出 -6</code></pre>
<pre class="brush:php;"><code class="language-php">
/* array */
echo floor(array('1'));//无输出</code></pre>
<pre class="brush:php;"><code class="language-php">
/* null */
echo floor(null);//输出 0</code></pre>
<pre class="brush:php;"><code class="language-php">
/* object or resource */
$a = new stdClass();
echo floor($a);
产生警告 Notice: Object of class stdClass could not be converted to int in XXXXX on line XX
?>
至此,关于 PHP floor() 函数的案例详解已介绍完毕。如需了解更多相关内容,欢迎搜索本站历史文章或继续浏览下方推荐内容,感谢大家的支持!