08c02-1.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 1000 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 1000 until it is less than 1000    $level = 0;    while ($bytes > 1000) {        $bytes /= 1000;        $level++;    }    // Now we have our final value, format it to just 1 decimal place    // and append on to 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.2 kBecho '<p>420000 bytes = ', format_bytes(420000), "<p>\n"; // 420 kBecho '<p>42000000 bytes = ', format_bytes(42000000), "<p>\n"; // 42 MB?>

⌨️ 快捷键说明

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