php intval的测试代码发现问题

yipeiwu_com6年前PHP代码库


<?php
$o = 0.1;
for($a = 1; $a < 100; $a++){
    $o += 0.1;
    echo "<br />intval('$o') = ".intval($o);
    if(intval($o)){
        print(" true");
    }else{
        print(" false");
    }
}
?> 

结果:
intval('0.2') = 0 false
intval('0.3') = 0 false
intval('0.4') = 0 false
intval('0.5') = 0 false
intval('0.6') = 0 false
intval('0.7') = 0 false
intval('0.8') = 0 false
intval('0.9') = 0 false
intval('1') = 0 false
intval('1.1') = 1 true
intval('1.2') = 1 true
intval('1.3') = 1 true
intval('1.4') = 1 true
intval('1.5') = 1 true
intval('1.6') = 1 true
intval('1.7') = 1 true
intval('1.8') = 1 true
intval('1.9') = 1 true
intval('2') = 2 true
intval('2.1') = 2 true
intval('2.2') = 2 true
intval('2.3') = 2 true
intval('2.4') = 2 true
intval('2.5') = 2 true
intval('2.6') = 2 true
intval('2.7') = 2 true
intval('2.8') = 2 true
..... 

发现 intval(1) 竟然返回 0 

不测试不会知道的

假象:
print("<br />intval(\"1.0\") = ".intval("1.0"));
print("<br />intval('1.0') = ".intval('1.0'));
print("<br />intval('1') = ".intval('1')); 

intval("1.0") = 1
intval('1.0') = 1
intval('1') = 1 

相关文章

PHP实现的简单缓存类

本文实例讲述了PHP实现的简单缓存类。分享给大家供大家参考。具体如下: cache.inc.php: <?php class Cache { /** * $di...

file_get_contents(&quot;php://input&quot;, &quot;r&quot;)实例介绍

file_get_contents(&quot;php://input&quot;, &quot;r&quot;)实例介绍

解释不清,直接上例子index.html复制代码 代码如下:  <form action="action.php" method="post" >  &l...

解析php如何将日志写进syslog

在做项目的时候们为了方便运维,我们经常需要将系统日志写入系统syslog,下边我们就介绍一下,在linux下php对syslog的操作:在linux中配置syslog在linux中,fa...

PHP购物车类Cart.class.php定义与用法示例

本文实例讲述了PHP购物车类Cart.class.php定义与用法。分享给大家供大家参考,具体如下: 之前的开发人员使用了JS的技术开发了一套前台购物车(删除添加什么的都使用JS),但是...

php按单词截取字符串的方法

本文实例讲述了php按单词截取字符串的方法。分享给大家供大家参考。具体分析如下: 这里指定字符串和单词数量进行截取 复制代码 代码如下:<?php function lim...