From: Ben Dunlap on
> If I put 0 filter_var() will return false.

Actually it returns the integer 0, not the boolean FALSE. Here's an
illustration of the difference:

http://codepad.org/73wff2u0

The integer value 0 can masquerade as "false" in an if() statement, of
course, as Ash pointed out above.

> If I put 0342352 filter_var() will also return false.

How is 0342352 being assigned to the variable that you're filtering?
If PHP thinks it's a string, then the filter will fail. If PHP thinks
it's a number, it seems to convert it internally to the number 115946,
before you get to the filter. Not sure what's going on there. At any
rate it will then pass FILTER_VALIDATE_INT, but the value's not going
to be what you expect. You can see it happening here:

http://codepad.org/tw2qlpC1

Ben
From: Ben Dunlap on
> How is 0342352 being assigned to the variable that you're filtering?
> If PHP thinks it's a string, then the filter will fail. If PHP thinks
> it's a number, it seems to convert it internally to the number 115946,
> before you get to the filter.  Not sure what's going on there. At any

Sorry, brain fart. In PHP, a leading 0 in an integer indicates an
octal number (thanks, Martin). PHP immediately converts it to decimal
internally. Hence 0342352 becomes 115946. But it's a bit of a fluke
that the example you used happened to be a valid octal number. Try
something that starts with 0 and has an 8 or a 9 in it; you'll end up
with plain old 0 (presumably because PHP's internal attempt to convert
from octal, fails):

http://codepad.org/KBUgAZWJ

Which, of course, leads to the apparent-false discussed above.

Ben
From: Ben Dunlap on
> How is 0342352 being assigned to the variable that you're filtering?
> If PHP thinks it's a string, then the filter will fail. If PHP thinks

Oops, potentially bad information there as well, sorry. In general, a
string representation of a decimal number /will/ pass
FILTER_VALIDATE_INT. But your particular string ("0342352") will only
fail FILTER_VALIDATE_INT in the filter's default configuration; set
the ALLOW_OCTAL flag and it will pass:

http://codepad.org/RNE5LZMr

You'll still end up with an unexpected value in your final variable, though.

Ben
From: Ashley Sheridan on
On Wed, 2009-10-07 at 12:03 +0100, MEM wrote:
> > Well, it was only a guess, but if you look at the integer limit on 32-bit systems, you'll see that the upper limit > for numbers is 2147483647 (or 2^31-1) which would explain maybe your upper limit problem.
> >
> > Also, I think you're getting confused over the zero with exactly what you are asking PHP to do. filter_var() returns > true if the filter matches. If the 0 match is returned as a false, then filter_var() will return false.. You're then > inverting that with a !, all of which is inside an if() statement. Essentially this would mean that if the filter > returns false then the instructions inside of the if statement are carried out.
>
>
> I always thought that that was a limit of the number of digits an integer value could have, and not the actually int value... I guess I was wrong. :s
>
> You are right, I've tested with:
> 2147483647
> it worked.
>
> I've tested with:
> 2147483648
> It displays the error.
>
> "you're getting confused over the zero with exactly what you are asking PHP to do"
> Absolutely... :(
>
> If I put 0 filter_var() will return false.
> If I put 0342352 filter_var() will also return false.
>
> Could we say that: if it is indeed the fact, that filter_var() returns false when it finds a 0 at the beginning of a given number...
>
> "then the instructions inside of the if statement are carried out."
>
> And here may be the reason for displaying the error message when we have the 0 leading a number.
>
> And my point was exactly here
> "If the 0 match is returned as a false"
>
> Why should filter_var() do something like this? Should the filter_var() interpretate 0 as a number without boolean semantic value?
>
>
> Please be patient...
> Regards,
> Márcio
>

Imho it shouldn't behave like this. 01 is equally as valid as 1 as far
as numbers go, we just ignore leading 0's on any number unless they are
in a significant position (i.e. 0.1)

I am also surprised to see that while it converts the value to an
integer, it doesn't seem to do that first, because 01 is a valid
integer:

<?php
$i = 0;
$j = 01;

var_dump($i);
var_dump($j);
?>

The output returns:

int(0)
int(1)

So the filter seems to be doing some form of check on leading zero's
before checking the validity of the number!

Thanks,
Ash
http://www.ashleysheridan.co.uk



From: Tom Worster on
On 10/7/09 6:04 AM, "Ashley Sheridan" <ash(a)ashleysheridan.co.uk> wrote:

> On Wed, 2009-10-07 at 10:57 +0100, MEM wrote:
>> Hello all,
>>
>>
>> I'm having this strange behavior, and I do not understanding why...
>>
>> When I use FILTER_VALIDATE_INT I'm unable to get, on my input box, values
>> starting with 0(zero), or values that are greater (in length) then 10
>> digits.
>>
>> I was expecting to be able to input the all range of integers.
>>
>>
>> I've tried this:
>>
>> if (!filter_var($telefone, FILTER_VALIDATE_INT))
>> {
>> $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
>> }
>>
>> And this:
>> if (!filter_var($telefone, FILTER_VALIDATE_INT, array("options" =>
>> array("min_range"=>-1, "max_range"=>9999999999999))))
>> {
>> $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
>> }
>>
>> And this:
>> if (filter_var($telefone, FILTER_VALIDATE_INT, array("options" =>
>> array("min_range"=>0, "max_range"=>99999999999999999))) == false )
>> {
>> $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
>> }
>>
>> No success. :s
>>
>> What am I not getting?
>>
>>
>> Thanks,
>> Márcio
>>
>>
>> Ps- Anyone knows where can we grab a good source information about
>> FILTER_VALIDATE_INT specifically?
>>
>>
>
> Well, at a guess, if a number is 0, PHP see's that as equivalent to a
> false. Also, numbers over 10 digits may be going over the limit for your
> PHP install, assuming PHP actually attempts to convert the string to a
> real integer for the purpose of the filter.

isn't a numeric string starting with 0 taken as octal? if so then 8 and 9
might cause validation errors.


> For things this simple, I've always tended to go with something like
> this:
>
> if(!preg_match('^[0-9 \+]$', $telefone))
> {
> $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
> }
>
> Which will check for all common types of phone numbers, including those
> where the user has put spaces in to separate groups of digits, and those
> that contain the + for country codes.

right. phone numbers are not integers. people also often use parentheses,
hyphens or dots in them.