Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Problem with Framework.Functions function ForceIncomingInt does not allow 0 value

edited June 2009 in Vanilla 1.0 Help
Hello,

I'm using this function, ForceIncomingInt, which should force integers to be 0 or greater. Unfortunately I have found that a function called from this function, ForceInt, prevents the incoming integer from being 0. The problem can be seen in the code:

function ForceInt($InValue, $DefaultValue) {
$iReturn = intval($InValue);
return ($iReturn == 0) ? $DefaultValue : $iReturn;

}

intval returns the integer value of $InValue, or it returns 0 for a failure case. SO if $InValue == 0 then the following result check will assume a fail and the $DefaultValue will be returned.

I guess that adding this line to the beginning of ForceInt will fix the problem:

if ($InValue == 0) return 0;

The workaround, if you need 0, is to set $DefaultValue to 0. Not ideal however the only way I can see to get around this.

Comments

  • I think it would need to be return ($InValue > 0 || $InValue === 0) ? $iReturn : $DefaultValue;

    PHP's typecasting would make your test return (int)0 even if the $InValue were (bool)false.
  • Yes, I would agree, but should we reference iReturn in the test?

    return ($iReturn > 0 || $InValue === 0) ? $iReturn : $DefaultValue;
Sign In or Register to comment.