08c02-2.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 25 行

PHP
25
字号
<?php// A function to format the bytes of a file, as a natural looking string.// This version handles it via 1024 based logic:function format_bytes($bytes) {	// Define an array of the different display forms:	$display = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');		// Now, constantly divide the value by 1024 until it is less than 1024	$level = 0;	while ($bytes > 1024) {		$bytes /= 1024;		$level++;	}	// Now we have our final value, format it to just 1 decimal place	// and append on to the the appropriate level moniker.	return round($bytes, 1) . ' ' . $display[$level];}// Echo out a few examples to test the function:echo '<p>4200 bytes = ', format_bytes(4200), "<p>\n"; // 4.1 kBecho '<p>420000 bytes = ', format_bytes(420000), "<p>\n"; // 410.2 kBecho '<p>42000000 bytes = ', format_bytes(42000000), "<p>\n"; // 40.1 MB?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?