How to use the microtime function in PHP?
The microtime function is used to retrieve the current Unix timestamp and microseconds, useful for conducting performance testing and timing.
The usage instructions are as follows:
$start = microtime(true);
// 执行一些代码
$end = microtime(true);
$time = $end - $start;
echo "执行时间:{$time} 秒";
In the example above, the function microtime(true) returns the current Unix timestamp and microseconds. The parameter true specifies that it returns the timestamp in float form, rather than as a string. The start time is saved in the variable $start, and after executing some code, the function microtime(true) is called again to get the end time and saved in the variable $end. The code execution time is then calculated by finding the difference between the two time values.
Finally, you can use the echo command to display the execution time on the screen.