PHP - 浮点数 - 逗号和点号问题

PHP - float numbers - comma and dot problem

我的 PHP 服务器 (5.6.40) 有问题。 PHP 有时会在没有任何警告的情况下将 更改为 逗号

例如:

$runTimes = 20;
$number   = 39.102564102564;

for ($i = 0; $i < $runTimes; $i++) {
    echo "<p>$number</p>";
}

echo 显示“39.102564102564”,但有时显示“39,102564102564”(带逗号)。这太糟糕了,因为当我将这个数字插入数据库时​​,它变成了 39(没有小数)。

我尝试了 setlocale(LC_NUMERIC, 'C'),但没有任何改变。

它不会发生在我的 localhost,只发生在我的服务器

localeconv() 打印:

Array
(
    [decimal_point] => .
    [thousands_sep] => 
    [int_curr_symbol] => 
    [currency_symbol] => 
    [mon_decimal_point] => 
    [mon_thousands_sep] => 
    [positive_sign] => 
    [negative_sign] => 
    [int_frac_digits] => 127
    [frac_digits] => 127
    [p_cs_precedes] => 127
    [p_sep_by_space] => 127
    [n_cs_precedes] => 127
    [n_sep_by_space] => 127
    [p_sign_posn] => 127
    [n_sign_posn] => 127
    [grouping] => Array
        (
        )

    [mon_grouping] => Array
        (
        )

)

编辑:

通过PHP(提示符)直接执行脚本不会发生。此问题仅通过浏览器(在本例中为 Firefox)发生

解决方案: 我换了一个叫prefork MPM的并发模型,问题解决了

区域设置不是 thread-safe PHP!

https://www.php.net/manual/en/function.setlocale.php

The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API , you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale(). On Windows, locale information is maintained per thread as of PHP 5.6.20 and PHP 7.0.5, respectively.

关于输出(更改点和逗号)- 这与 C-level 上的 number-to-string 转换有关(PHP 是用 C 编写的...)。通过一些研究,您可能会无意中发现这个网站。

https://phpinternals.news/52

最初的问题是:另一个脚本,运行 在与您的进程相同的进程中,在您的脚本执行时更改语言环境...即使这个其他脚本可能会将语言环境切换回其原始值, 尽管它可能只有几分之一秒被更改,但它仍然在进程的所有线程之间共享。

hth