0
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

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
39467
  • 13
  • 6
  • 3
    do they only return the same value or are they identical functions with different names? If so, why do you need both? – ithil Mar 15 '16 at 13:35
  • 1
    the functions are identical, see here http://stackoverflow.com/questions/1688711/can-we-alias-a-function-in-php how to alias one function name to the other if you don't want to change function names through the whole code – Alex Andrei Mar 15 '16 at 13:36
  • @AlexAndrei why would we alias these functions if they do the exact same thing? Im just trying to understand the reasoning for this. – CodeGodie Mar 15 '16 at 13:39
  • if both function names have been used in the code-base and the OP doesn't want to refactor – Alex Andrei Mar 15 '16 at 13:40
  • I see, yeah I guess that could be useful in that scenario. Thanks. – CodeGodie Mar 15 '16 at 13:41
  • Best way: rename all your function calls (with a good text editor it is very simple). Otherwise: on php >= 5.6: http://stackoverflow.com/questions/1688711/can-we-alias-a-function-in-php / on PHP < 5.6: `function images_links_fileSize($filesize) { return doc_links_fileSize($filesize); }` – fusion3k Mar 15 '16 at 13:43

2 Answers2

2

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';
}
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

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.

B.Kocaman
  • 800
  • 4
  • 13