3v4l.org

run code in 300+ PHP versions simultaneously
<?php function file_format_size($bytes, $decimals = 2) { $unit_list = array('B', 'KB', 'MB', 'GB', 'PB'); if ($bytes == 0) { return $bytes . ' ' . $unit_list[0]; } $unit_count = count($unit_list); for ($i = $unit_count - 1; $i >= 0; $i--) { $power = $i * 10; if (($bytes >> $power) >= 1) return round($bytes / (1 << $power), $decimals) . ' ' . $unit_list[$i]; } } foreach ([0, 10, 100, 1000, 1024, 10000, 100000, 1000000, 10000000] as $bytes) { var_export(file_format_size($bytes)); echo "\n"; }
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.30, 8.0.0 - 8.0.20, 8.1.0 - 8.1.7
'0 B' '10 B' '100 B' '1000 B' '1 KB' '9.77 KB' '97.66 KB' '976.56 KB' '9.54 MB'

preferences:
98.47 ms | 409 KiB | 5 Q