Hi, 
 
you can improve the test if you write

function myIsInt($value) {
        $temp = (int) $value;
        return  ((string)$temp) === ((string)$value) ;
}

instead of

function myIsInt($value) {
        $temp = (int) $value;
        return  ((string)$temp) == ((string)$value) ;
}
 
Seems to me as if php converts the string to an int if the string looks like an 
int, even if you tell the interpreter to regard the value as string - bug??. 
This change makes all "non-int"-test pass but does not fix the problem of 
"1.002e9" which is indeed a valid int. However, if you have a form where 
someone enters integers like that, you're in a very special problem domain - 
most probably you're writing some sort of math app. You can't stuff that value 
in a database field either, it would need to be converted, so, following the 
priciple of least surprise I think that a validator should not let these values 
pass unless specifically told to do so. 
Typecast checking has some other implications, though: It checks wether the 
value is actually within the range of valid php-integers. If the number entered 
cannot be processed by php, the test will fail - a numeric input of 
"12345678901234567890" will not pass the test as it exceeds php's int size. 
This behaviour might be desired (if you wish to do some math with the number) 
or not (if you just want to stuff this in a DB).
 
regards
 
fg

________________________________

Von: [EMAIL PROTECTED] im Auftrag von Veikko Mäkinen
Gesendet: Fr 28.10.2005 15:20
An: Agavi Development
Betreff: Re: AW: [agavi-dev] some fixes & improvements



Felix Gilcher wrote:
> Hi,
>
> caution, there is a bug in that diff: the elseif condition will
> always be true as it assigns the value 'int' to the variable $type
> which should always succeed.

Ups, busted... didn't test my code before sending :)

> assignment]. A second (but minor thing): I'd rather use typecasts
> than regexps for this kind of checking, as regular expressions carry
> the overhead of invoking the regexp engine.

I'd rather use typecasts too, but I'm yet to see a typecast test that
wouldn't let some obvious non-integers pass the test. I took our test
and tried to make it work but wasn't able...

test:
http://veikkomakinen.com/temp/test2.txt

result:
http://veikkomakinen.com/temp/result.txt


-veikko

_______________________________________________
agavi-dev mailing list
[email protected]
http://labworkz.com/cgi-bin/mailman/listinfo/agavi-dev


<<winmail.dat>>

_______________________________________________
agavi-dev mailing list
[email protected]
http://labworkz.com/cgi-bin/mailman/listinfo/agavi-dev

Reply via email to