note 106326 deleted from function.microtime by danbrown
| From: | danbrown@php.net | Date: | Thu, 27 Oct 2011 21:50:29 +0000 |
| Subject: | note 106326 deleted from function.microtime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184186@lists.php.net to get a copy of this message | ||
Note Submitter: Cyberknight
----
The code example by Paul (21-Aug-2010 06:44) has a bug that causes it to generate negative values
when calculating time elapsed:
<?php
//This will produce a negative value:
$start = microtime();
for($i=100;$i>0;$i--){
echo $i;
}
$end = microtime();
echo 'Took:'.$end-$start.' seconds';
?>
Fixed code:
<?php
//This will parse correctly:
$start = microtime();
for($i=100;$i>0;$i--){
echo $i;
}
$end = microtime();
echo 'Took:'.($end-$start).' seconds';
?>
The first code will produce "Took:".$end minus $start." seconds" string
evaluation, the second will produce the expected elapsed time and print it in a string. Parenthesis
are your friends, use them, don't expect the parser will understand what you want, tell it
exactly what you want.