note 97946 added to function.memory-get-usage

From: Date: Tue, 18 May 2010 00:12:53 +0000
Subject: note 97946 added to function.memory-get-usage
Groups: php.notes 
Request: Send a blank email to php-notes+get-169334@lists.php.net to get a copy of this message
xelozz -at- gmail.com's neat "convert" function below fails for negative, and/or zero sizes. To fix the negatives, simply wrap the $size that is inside the log function call, with an abs function call. eg: return @round($size/pow(1024,($i=floor(log( abs($size) ,1024)))),2).' '.$unit[$i]; To fix the 0 $size issue (which causes log to return -INF), just force $size to be non-zero within the log/abs call. eg: return @round($size/pow(1024,($i=floor(log( abs($size==0?1:$size) ,1024)))),2).' '.$unit[$i]; So, the final code could look like: <?php function convert($size) { $unit=array('b','kb','mb','gb','tb','pb'); return @round($size/pow(1024,($i=floor(log( abs($size==0?1:$size) ,1024)))),2).' '.$unit[$i]; } echo convert(memory_get_usage(true)); // 123 kb echo convert(-10240); // -10 kb echo convert(0); // 0 b ?> ---- Server IP: 117.104.162.229 Probable Submitter: 58.96.72.24 (proxied: 58.96.72.24) ---- Manual Page -- http://www.php.net/manual/en/function.memory-get-usage.php Edit -- https://master.php.net/note/edit/97946 Del: integrated -- https://master.php.net/note/delete/97946/integrated Del: useless -- https://master.php.net/note/delete/97946/useless Del: bad code -- https://master.php.net/note/delete/97946/bad+code Del: spam -- https://master.php.net/note/delete/97946/spam Del: non-english -- https://master.php.net/note/delete/97946/non-english Del: in docs -- https://master.php.net/note/delete/97946/in+docs Del: other reasons-- https://master.php.net/note/delete/97946 Reject -- https://master.php.net/note/reject/97946 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169334) next »