RE: [PHP] FILTER_VALIDATE_INT - newbie question
| From: | Ashley Sheridan | Date: | Wed, 07 Oct 2009 11:14:15 +0000 |
| Subject: | RE: [PHP] FILTER_VALIDATE_INT - newbie question | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298741@lists.php.net to get a copy of this message | ||
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