In PHP, you can convert bytes into kilobytes (KB), megabytes (MB), and gigabytes (GB) by dividing the given number of bytes by the corresponding factor.
In this example, I will show you how to convert bytes to kb mb gb in php
<?PHP
function formatBytes($bytes, $precision = 2) {
$kilobyte = 1024;
$megabyte = $kilobyte * 1024;
$gigabyte = $megabyte * 1024;
if ($bytes < $kilobyte) {
return $bytes . ' B';
} elseif ($bytes < $megabyte) {
return round($bytes / $kilobyte, $precision) . ' KB';
} elseif ($bytes < $gigabyte) {
return round($bytes / $megabyte, $precision) . ' MB';
} else {
return round($bytes / $gigabyte, $precision) . ' GB';
}
}
/* Example usage: */
$bytes = 567890123; /* Replace this with the actual number of bytes you have */
echo formatBytes($bytes);
?>

