Home  >  Article  >  Backend Development  >  生手学php的疑问(周末结贴)

生手学php的疑问(周末结贴)

WBOY
WBOYOriginal
2016-06-13 12:18:22917browse

新手学php的疑问(周末结贴)
echo (int) ( (0.1+0.7) * 10 ); // 显示 7
echo (int) ( (0.2+0.7) * 10 ); // 显示 9
?>
第一条的执行结果为什么不是8
------解决思路----------------------
浮点数的精度问题

 printf('%0.16f',  (0.1+0.7) * 10 ); // 7.9999999999999991<br /><br />

------解决思路----------------------

本帖最后由 xuzuning 于 2015-04-13 11:10:30 编辑 那是你理解错了
(int) 强制转换成整数,此时只将小数部分截掉,并无其他处理
echo (int)2.2; //2
echo (int)2.9; //2
同理
echo (int)7.9999999999999991; //7

echo (0.1+0.7) * 10; //8
是因为 php 按有效数规则做了处理
------解决思路----------------------
浮點數精度問題。
http://segmentfault.com/q/1010000002123613
------解决思路----------------------
打开错误日志。
------解决思路----------------------
調試建議安裝xdebug

http://www.cnblogs.com/qiantuwuliang/archive/2011/01/23/1942382.html
------解决思路----------------------
$this 是实例化后的对象

A::foo(); 是以静态方式调用类的方法
php 5.3 以后将会有一个 Strict Standards 级别的错误警告:
Non-static method A::foo() should not be called statically 
不能以静态方式调用非静态方法

既然你是初学,建议使用的 php 版本为 5.4 及以上,错误检查级别为全部
这样比较容易形成良好的习惯
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn