note 106326 added to function.microtime
| From: | Cyberknight at osu1 dot php dot net | Date: | Thu, 27 Oct 2011 21:49:16 +0000 |
| Subject: | note 106326 added to function.microtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184185@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 200.252.135.73
----
Manual Page -- http://www.php.net/manual/en/function.microtime.php
Edit -- https://master.php.net/note/edit/106326
Del: integrated -- https://master.php.net/note/delete/106326/integrated
Del: useless -- https://master.php.net/note/delete/106326/useless
Del: bad code -- https://master.php.net/note/delete/106326/bad+code
Del: spam -- https://master.php.net/note/delete/106326/spam
Del: non-english -- https://master.php.net/note/delete/106326/non-english
Del: in docs -- https://master.php.net/note/delete/106326/in+docs
Del: other reasons-- https://master.php.net/note/delete/106326
Reject -- https://master.php.net/note/reject/106326
Search -- https://master.php.net/manage/user-notes.php