[Solved] If and If

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frozenarmageddon
Forum Newbie
Posts: 19
Joined: Wed Aug 05, 2009 6:29 pm

[Solved] If and If

Post by frozenarmageddon »

Hey there, I am trying to make an Auto bytes converter, so it won't display the size as 45278489 [bytes] but as 45.278 MB.
The math part works flawless, but I got a problem with the IF statements...
I tried like 7 different ways to write it and none worked.
My most recent try:

Code: Select all

if($filesize <= "500000000") and if($filesize > "999999")
    {
        $filesizecalc = (round($filesize / 1000) / 1000);
        $filesizebyte = "MB";
    }
    else if($filesize <= "1000000") and if($filesize > "999")
    {
        $filesizecalc = ($filesize / 1000);
        $filesizebyte = "KB";
    }
I also tried stuff like

Code: Select all

if("999999" <= $filesize <= "500000000")
Most of my tries worked [No errors] but they didn't work as I wanted them to. (The way I wanted them to work is in the complete tagged code [not the last one, the one before.] which is pretty self explanatory), they just ignored the other else if's, and used the $filesizecalc and $filesizebyte setting of the first one...

I think I know a way to make it work, but that way is going to be really a pain to read and understand, and also pretty much double the size of the file, and make it messy...
I really appreciate any help, even if you say you don't know :D
Oh, and sorry for the bad English, I just woke up after a whole night working on this, and my brain is still kinda numb... :|
Last edited by frozenarmageddon on Tue Sep 08, 2009 4:13 pm, edited 1 time in total.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: If and If

Post by cpetercarter »

Code: Select all

 
    if ($filesize <= 500000000 && $filesize > 999999)
        {
            $filesizecalc = (round($filesize / 1000) / 1000);
           $filesizebyte = "MB";
        }
        elseif ($filesize <= 1000000 && $filesize > 999)
        {
            $filesizecalc = ($filesize / 1000);
            $filesizebyte = "KB";
       }
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: If and If

Post by requinix »

Would you mind using 1024 instead of 1000? This whole kilo/kibi thing is kinda silly.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: If and If

Post by Mark Baker »

Code: Select all

 
function sizeIt($value) {
    $nameArray = array('bytes','KB','MB','GB','TB','PB');
 
    $i = 0;
    while ($value > 1024) {
        $i++
        $value /= 1024;
    }
    return $value.' '.$nameArray[$i];
}
 
frozenarmageddon
Forum Newbie
Posts: 19
Joined: Wed Aug 05, 2009 6:29 pm

Re: If and If

Post by frozenarmageddon »

Thanks cpetercarter, I forgot about the &&, and I see you fixed not just it so thanks a lot <3
Thanks Mark Baker for the function, I think I won't use it though because I need it for only this page momentary which makes defining a function pretty useless at the moment ^^
And thanks tasairis for reminding me that I am using Binaries not SI units lol
You made me want check Wikipedia for it, and made me discover that after Petabytes there are Exabytes, Zettabytes and Yottabytes so special thanks to you too <3
Post Reply