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下编码转换函数mb_convert_encoding与iconv的使用说明

不过英文一般不会存在编码问题,只有中文数据才会有这个问题。比如你用Zend Studio或Editplus写程序时,用的是gbk编码,如果数据需要入数据库,而数据库的编码为utf8时,这...

PHP中filter函数校验数据的方法详解

介绍PHP中filter函数校验数据的方法详解,PHP过滤器包含两种类型:Validation用来验证验证项是否合法 、Sanitization用来格式化被验证的项目,因此它可能会修改验...

手把手教你使用DedeCms V3的在线采集图文教程

手把手教你使用DedeCms V3的在线采集图文教程

这是我们要采集的目标网址-------------------------------------------------------------screen.width*0.7) {t...

支持中文字母数字、自定义字体php验证码代码

复制代码 代码如下: <?php /* * Captcha Class base on PHP GD Lib * @author Design * @version 1.0 * @...

php无限级分类实现方法分析

本文实例讲述了php无限级分类实现方法。分享给大家供大家参考,具体如下: 1. 递归 public function getInfo(){ $data=$this->selec...