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"; }

preferences:
59.71 ms | 402 KiB | 5 Q