Set printf () to PHP variable

I have a php script that show a time like: 9.08374786377E-5, but I need a simple floating value like time, like: 0.00009083747 ..... thats why I just print it with float:

<?php function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $time_start = microtime_float(); $time_end = microtime_float(); $time = $time_end - $time_start; printf('%.16f', $time); ?> 

It shows the result beautifully, but I need to set this print value in a new variable. How can i do this? I need to set this print value in the new variable $ var;

 $var = printf('%.16f', $time); 

// We all know that it does not work, but how to install?

+6
source share
2 answers

You need to use the sprintf command to get your data as a variable ... printf displays the results, while sprintf returns the results

 $var = sprintf('%.16f', $time); 
+25
source

This is because sprintf() returns a string, printf() displays it.

 printf('%.16f', $time); 

matches with:

 sprintf('%.16f', $time); 

Since sprintf() outputs the result to a string, you can save it in a variable as follows:

 $var = sprintf('%.16f', $time); 

Hope this helps!

+4
source

Source: https://habr.com/ru/post/949255/


All Articles