Reputation: 13
function doc_links_fileSize($filesize){
if ($filesize < 1024) return $bytes.'B';
elseif ($filesize < 1048576) return round($filesize / 1024, 2).'KB';
elseif ($filesize < 1073741824) return round($filesize / 1048576, 2).'MB';
elseif ($filesize < 1099511627776) return round($filesize / 1073741824, 2).'GB';
else return round($bytes / 1099511627776, 2).'TB';
}
function images_links_fileSize($filesize){
if ($filesize < 1024) return $bytes.'B';
elseif ($filesize < 1048576) return round($filesize / 1024, 2).'KB';
elseif ($filesize < 1073741824) return round($filesize / 1048576, 2).'MB';
elseif ($filesize < 1099511627776) return round($filesize / 1073741824, 2).'GB';
else return round($bytes / 1099511627776, 2).'TB';
}
How can i combine two function which returns same value .I want to reduce the code by combining two functions ,as the both functions return same value
Upvotes: 0
Views: 63
Reputation: 815
A function can call another one. Like this,
<?php
function doc_links_fileSize($filesize){
if ($filesize < 1024) return $bytes.'B';
elseif ($filesize < 1048576) return round($filesize / 1024, 2).'KB';
elseif ($filesize < 1073741824) return round($filesize / 1048576, 2).'MB';
elseif ($filesize < 1099511627776) return round($filesize / 1073741824, 2).'GB';
else return round($bytes / 1099511627776, 2).'TB';
}
function images_links_fileSize($filesize){
return doc_links_fileSize($filesize);
}
?>
but the best way like @RomanPerekhrest said.
Upvotes: 1
Reputation: 92854
Just use a single function and give it a more common name(which will cover all docs, images etc.)
function getFileSize($filesize){
if ($filesize < 1024) return $bytes.'B';
elseif ($filesize < 1048576) return round($filesize / 1024, 2).'KB';
elseif ($filesize < 1073741824) return round($filesize / 1048576, 2).'MB';
elseif ($filesize < 1099511627776) return round($filesize / 1073741824, 2).'GB';
else return round($bytes / 1099511627776, 2).'TB';
}
Upvotes: 2