Sometimes we need to show the size of a file on a Web page, or the size or number of other data.
This number is often from a large span, if the unit B is a bit, if 1G is up to 1073741824 of the number, this time we need to format according to the size, such as less than 1K is displayed in units B, less than 1M is displayed in kilobytes, less than 1G is displayed in megabytes, etc...
The Format function reference is as follows:
//Format size DisplayfunctionFormatsize ($b,$times=0){ if($b>1024){ $temp=$b/1024; returnFormatsize ($temp,$times+1); }Else{ $unit= ' B '; Switch($times){ Case' 0 ':$unit= ' B '; Break; Case' 1 ':$unit= ' KB '; Break; Case' 2 ':$unit= ' MB '; Break; Case' 3 ':$unit= ' GB '; Break; Case' 4 ':$unit= ' TB '; Break; Case' 5 ':$unit= ' PB '; Break; Case' 6 ':$unit= ' EB '; Break; Case' 7 ':$unit= ' ZB '; Break; default:$unit= ' Unit unknown '; } return sprintf('%.2f ',$b).$unit; }}
Call:
echo formatsize (' 20667564 ');
The result is:
19.71MB
Description
Where the parameter $b is a number in B, $times is used to identify how many times the function is recursive.
The non-commonly used units TB, PB, EB, ZB please refer to the following notes (from the network):
1bit (this bit represents a binary digit)1Byte (this word transliteration is also called "bit" but represents a hexadecimal number)
1b=1byte=8bit
1 KB = 1024x768 B (kb-kilobyte) thousand
1 MB = KB (mb-megabyte) trillion
1 GB = 1024x768 (gb-gigabyte)
1 TB = 1024x768 (tb-terabyte) too
1 PB = 1024x768 TB (pb-petabyte) Pat
1 EB = 1024x768 PB (eb-eksabyte) AI
1 ZB = Zb-zettabyte (EB)
1 YB = 1024x768 ZB (yb-yottabyte) Woo
1 BB = YB (bb-brontobyte)
Yotta, Yao [it], Y. 10^21,
Zetta, ze [it], Z. 10^18,
Exa, Ai [sa], E. 10^15,
PETA, Pat [it], P. 10^12,
Tera, too [pull], T. 10^9,
Giga, Ji [Cafe], G. 10^6,
Mega, trillion, M. 10^3
The above describes the PHP format (file) storage data size (sizes) display, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.