search

Home  >  Q&A  >  body text

Why are the two return methods of the microtime() function in PHP different in precision?

Environment:
CentOS 7.3.1611
PHP 7.1.3

Code:

<?php
var_dump(microtime());
var_dump(microtime(true));

Output:

string(21) "0.23337700 1495449772"
float(1495449772.2334)

Question:
Run the code repeatedly and get the same result.
It is easy to understand that it is easy to understand to output an 8-digit millisecond value in the form of a string. Anyway, the last two digits are always 0. When output as a floating point number, the accuracy is controlled to four digits. What is the reason?

Additional verification:
Execute php -r "echo ini_get('precision'), PHP_EOL;"The output result is 14, which is obviously not about floating point in PHP The precision setting affects the output of microtime(). The "microtime" keyword is not found in php.ini. It seems that there are no special settings that affect the output accuracy.

I'm dizzy, and I'm looking forward to the expert's advice.

習慣沉默習慣沉默2748 days ago619

reply all(1)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-24 11:35:37

    ini_get('precision') A control accuracy of 14 means:

    float(1495449772.2334) has 14 digits in total, 10 digits before the decimal point and 4 digits after the decimal point. It does not mean 14 digits after the decimal point.

    Let’s take a look at the results of the facet code.

    ini_set('precision', 18);
    var_dump(microtime());
    var_dump(microtime(true));

    reply
    0
  • Cancelreply