note 123305 added to function.log

From: Date: Mon, 05 Nov 2018 01:28:06 +0000
Subject: note 123305 added to function.log
Groups: php.notes 
Request: Send a blank email to php-notes+get-209613@lists.php.net to get a copy of this message
Seems like unit prefixes should have a standard PHP function. Maybe in the future. I found this page while looking for a quick unit prefix function. The one by olafurw was voted down, I think because it had unchecked array indexes and /0s. So here it is fixed and readable. -Should work down to PHP 4. -Not meant for fractions or negatives, so anything less than 1 returns 0. -Not very effective on really really large numbers, but it's easy to add more prefixes. -Doesn't handle non numeric arguments. PHP 7+ can do this: function binaryprefix( int $units, $unit = '' ) // returns reduced $units with a binary prefix // ex. ( 110974120, 'B' ) == 105.8MiB // ex. ( 2^100, 'B' ) == 1048576.0YiB // ex. ( 0.12314, 'B' ) == 0B function binaryprefix( $units, $unit = '' ) { $prefix = array('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'); $exponent = min( floor( log( max( 1, $units ), 1024 ) ), count( $prefix ) - 1 ); if ( $units < 1024 ) return sprintf( '%d%s%s', max( 1, $units + 1 ) - 1, $prefix[$exponent], $unit ); else return sprintf( '%.1f%s%s', $units / pow(1024, $exponent), $prefix[$exponent], $unit ); } https://en.wikipedia.org/wiki/Binary_prefix#Adoption_by_IEC.2C_NIST_and_ISO Also more colloquially: $prefix = array('', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y'); ---- Server IP: 208.43.231.9 Probable Submitter: 97.120.183.108 ---- Manual Page -- http://php.net/manual/en/function.log.php Edit -- https://master.php.net/note/edit/123305 Del: integrated -- https://master.php.net/note/delete/123305/integrated Del: useless -- https://master.php.net/note/delete/123305/useless Del: bad code -- https://master.php.net/note/delete/123305/bad+code Del: spam -- https://master.php.net/note/delete/123305/spam Del: non-english -- https://master.php.net/note/delete/123305/non-english Del: in docs -- https://master.php.net/note/delete/123305/in+docs Del: other reasons-- https://master.php.net/note/delete/123305 Reject -- https://master.php.net/note/reject/123305 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#209613) next »