Not signed in (Sign In)

Categories

Vanilla 1.1.4 is a product of Lussumo. More Information: Documentation, Community Support.

Help keep Vanilla free:
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    • CommentAuthorgerry22
    • CommentTimeSep 1st 2007 edited
     # 1
    lines 377 -385 Framework.Functions:

    function FormatFileSize($FileSize) {
    if ($FileSize > 1048576) {
    return intval((($FileSize / 1048576) * 100) + 0.5) / 100 . "mb";
    } elseif ($FileSize > 1024) {
    return ceil($FileSize / 1024)."kb";
    } else {
    return $FileSize."b";
    }
    }


    this can't be right. I use phpDesigner and found that if i beautified this script i get a parse error, it seems that the space is required around the period that seperates 100 and "mb"

    this will cause a parse error:
    return intval((($FileSize / 1048576) * 100) + 0.5) / 100."mb";


    i think it should be something like:
    return (intval((($FileSize / 1048576) * 100) + 0.5) / 100)."mb";

    or
    return intval(((($FileSize / 1048576) * 100) + 0.5) / 100)."mb";
    •  
      CommentAuthorDinoboff
    • CommentTimeSep 1st 2007 edited
     # 2
    I just tested it (php 4.4.4 and php 5.2.3) and it seems to works.
    Here the simpleTest test:

    function testFormatFileSize() {
    $FileSize = 1;

    $TestSize = $FileSize*1024*1024;
    $Result = FormatFileSize($TestSize);
    $this->assertIdentical("1024kb", $Result);

    $TestSize = $FileSize*1024;
    $Result =FormatFileSize($TestSize);
    $this->assertIdentical( "1024b", $Result);

    $FileSizes = array(1.005, 2, 500);
    foreach ($FileSizes as $FileSize) {
    $TestSize = $FileSize*1024*1024;
    $Result = FormatFileSize($TestSize);
    $this->assertIdentical(round($FileSize, 2)."mb", $Result);

    $TestSize = $FileSize*1024;
    $Result =FormatFileSize($TestSize);
    $this->assertIdentical( ceil($FileSize)."kb", $Result);

    $TestSize = intval($FileSize);
    $Result = FormatFileSize($TestSize);
    $this->assertIdentical(intval($FileSize)."b", $Result);
    }
    }
    • CommentAuthorithcy
    • CommentTimeSep 1st 2007 edited
     # 3
    Dinoboff, i don't think the error will show up when you use functions in your test like intval($x)."mb".
    the issue arises when numeric constants are used with the string concatenation operator (.)
    php thinks that 100. means "100 decimal point", not "100 concatenated with"
    and so it is expecting a number after the . like 100.56, instead of a string constant like 100."mb".
    therefore it tries to do number math with a string constant and errors out.

    this test generates an error:
    print "php thinks that 100.\"mb\" is " . 100."mb";>> Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in numtest.php on line 2

    this test does not:
    print "php thinks that 100 .\"mb\" is " . 100 ."mb";>> php thinks that 100 ."mb" is 100mb

    note that the only difference between those two tests is the space after the 100.

    i think this is a valid bug. HOWEVER, the bug is in PHP Designer's beautify function, NOT in vanilla. these conditions do not occur in Framework.Functions.php.

    gerry22, if you run a code beautifying tool on code that you know is working, and then it stops working, the problem is most likely in the tool, not in the pre-beautified code.
    • CommentAuthorgerry22
    • CommentTimeSep 3rd 2007
     # 4
    with my limited knowledge of php, this now looks like a bug in php itself.

    lets forget about phpDesigner and beautifiers. And say we go in manually to Framework.Functions.php and edit the line:
    return intval((($FileSize / 1048576) * 100) + 0.5) / 100 . "mb";
    to
    return intval((($FileSize / 1048576) * 100) + 0.5) / 100."mb";
    i.e. (stripping the "whitespace" around the fullstop(.) which is being used as an operator concatenating two strings, two variables or a string and variable.)

    we get a parse error, BUT shouldn't we be able to strip away what is essentially white space?
    And, is it valid to concatenate numbers to strings, which php seems to be allowing?
Add your comments
    Username Password
  • Format comments as