PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 14, 2019 12:04 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Aug 03, 2010 11:59 am 
Offline
Forum Newbie

Joined: Thu Mar 19, 2009 2:12 am
Posts: 18
I was curious if there are other/better ways to code the following (it's a function to convert data sizes):

Syntax: [ Download ] [ Hide ]
    function convert_data( $number, $unit0, $unit1 )
    {   // Base unit is megabytes
        $d = array();
        $d['bit']                   = 1/(2^23);
        $d['b'] = $d['byte']        = 1/(2^20);
        $d['kbit'] = $d['kilobit']  = 1/(2^13);
        $d['kb'] = $d['kilobyte']   = 1/(2^10);
        $d['mbit'] = $d['megabit']  = 1/(2^3);
        $d['mb'] = $d['megabyte']   = 2^0;
        $d['gbit'] = $d['gigabit']  = 2^3;
        $d['gb'] = $d['gigabyte']   = 2^10;
        $d['tbit'] = $d['terabit']  = 2^13;
        $d['tb'] = $d['terabyte']   = 2^20;
        $d['pbit'] = $d['petabit']  = 2^23;
        $d['pb'] = $d['petabyte']   = 2^30;
        $d['ebit'] = $d['exabit']   = 2^33;
        $d['eb'] = $d['exabyte']    = 2^40;

        $t = (array_key_exists($unit0, $d) AND array_key_exists($unit0, $d))? ($number * $d[$unit0]) / $d[$unit1]: FALSE;
        unset($d);
        return $t;
    }


thanks!


Top
 Profile  
 
PostPosted: Sun Aug 08, 2010 7:51 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
It isn't clear from the function's signature what the parameters are supposed to be for.

Imagine seeing this in your code complete
function convert_data( $number, $unit0, $unit1 )

Maybe its from and to? do I pass in some sort of integer? A string? Which is which? Am I even correct in my assumption. The code itself looks fine I didn't read it in detail. I could probably figure out how to use it if I read it more, but that means the code can be improved - so I think I've looked at it enough to offer my opinion. Looks decent. Could have more descriptive & obvious signature.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group