<分区>
很简单。考虑这段代码:
var_export (11.2);
返回
11.199999999999999
使用 PHP 5.6
怎么回事?
<分区>
很简单。考虑这段代码:
var_export (11.2);
返回
11.199999999999999
使用 PHP 5.6
怎么回事?
最佳答案
来自 php.net 手册页的评论:
Looks like since version 5.4.22 var_export uses the serialize_precision ini setting, rather than the precision one used for normal output of floating-point numbers. As a consequence since version 5.4.22 for example var_export(1.1) will output 1.1000000000000001 (17 is default precision value) and not 1.1 as before.
很高兴知道。我也不知道这个变化。
Available since PHP 4.3.2. Until PHP 5.3.5, the default value was 100.
因此,我们可以获得我们熟悉的行为:ini_set('serialize_precision', 100)
;
使用 ini_set() 时要非常小心,因为这可能会进一步改变代码的行为。一个“安全”的方法是使用这样的东西:
$storedValue = ini_get('serialize_precision');
ini_set('serialize_precision', 100);
// Your isolated code goes here e.g var_export($float);
ini_set('serialize_precision', $storedValue);
这可确保代码中进一步向下/更深层次的更改不受影响。
通常,在您的代码中使用 ini_set() 应该被认为是危险的,因为 它可能会产生严重的副作用。重构您的代码以在没有 ini_set() 的情况下运行通常是更好的选择。
关于php,var_export 失败并出现 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32149340/