From: Ben Dunlap on
> 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

filter_var() actually returns the filtered data if the filter matches,
and FALSE if it doesn't. That's the whole point of the filter_XXX
functions; to pass a tainted value through a filter and get a clean,
"safe" value out the other end:

$tainted = get_user_input();
$clean = filter_var($tainted, [FILTER_CONSTANT]);
// now use $clean and never touch $tainted again

From the original code above, it looks like the OP was
misunderstanding the use of filter_var() and expecting it to return a
boolean.

Ben
From: "MEM" on
> Well, at a guess, if a number is 0, PHP see's that as equivalent to a
> false.

In this case, shouldn't php ignore the 0 as 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.

This happens with two different php configurations... Is it common to have this kind of limit on php configuration?
Is this something that I need to be aware of?


>
> For things this simple,
THIS IS NOT SIMPLE !!! :) Newbie blood is in there! :p
Kidding. ;)

> 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.

I will absolutely considerer the use of your regex. Thanks a lot.


Before your e-mail, I've considering the use of this:
if (!is_numeric($telefone))
{
$erros['numberofsomething'] = 'Error - only numbers please.';
}

In the case we need to validate other ints, that are not phones for example, only an unlimited range of ints,
and we want to store that values on a database, should we have special considerations regarding the use of is_numeric ?



Thanks a lot,
Márcio

From: Martin Scotta on
Are you using the == operator or === ?
maybe that's the problem

the function could return false or 0. You need to use the === operator

if( false !== ( $value = filter_var($value, FILTER_VALIDATE_INT)))
{
echo 'I need an Integer';
exit;
}
echo 'Thanks for the Int';

On Wed, Oct 7, 2009 at 2:13 PM, Ben Dunlap <bdunlap(a)agentintellect.com>wrote:

> > 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
>
> filter_var() actually returns the filtered data if the filter matches,
> and FALSE if it doesn't. That's the whole point of the filter_XXX
> functions; to pass a tainted value through a filter and get a clean,
> "safe" value out the other end:
>
> $tainted = get_user_input();
> $clean = filter_var($tainted, [FILTER_CONSTANT]);
> // now use $clean and never touch $tainted again
>
> From the original code above, it looks like the OP was
> misunderstanding the use of filter_var() and expecting it to return a
> boolean.
>
> Ben
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Martin Scotta
From: "MEM" on
Thank you all for your replies.

The issue was "solved" for a better fitting solution on this case, that was
by using a regex expression.

I've not tested the difference between using == or ===. I was using !

But, the main question that is not clear on my newbie head is, "why how why"
couldn't this 0 be interpreted, neither as octal, or false, or whatever it
may be, but as just a humble numeric value of 0. At least on the cases where
FILTER_VALIDADE_INT is there.

Anyway, I cannot dig in into this because I'm actually unable to offer some
solutions.

So, as stated, the issue was solved, by using a regex, so all good here. ;)

Thanks once again,
Márcio