PHP7.1 json_encode() Float Issue

后端 未结 11 1025
既然无缘
既然无缘 2020-11-27 13:00

This isn\'t a question as it is more of a be aware. I updated an application that uses json_encode() to PHP7.1.1 and I was seeing an issue with floats being cha

11条回答
  •  一向
    一向 (楼主)
    2020-11-27 13:32

    As a plugin developer I don't have general access to the php.ini settings of a server. So, based on Machavity's answer I wrote this small piece of code that you can use in your PHP script. Simply put it on top of the script and json_encode will keep working as usual.

    if (version_compare(phpversion(), '7.1', '>=')) {
        ini_set( 'serialize_precision', -1 );
    }
    

    In some cases it is necessary to set one more variable. I am adding this as a second solution because I am not sure if the second solution works fine in all cases where the first solution has proven to work.

    if (version_compare(phpversion(), '7.1', '>=')) {
        ini_set( 'precision', 17 );
        ini_set( 'serialize_precision', -1 );
    }
    

提交回复
热议问题