Re: [PHP] Validating form field text input to be a specificvariable type
From: William Lovaton [EMAIL PROTECTED] Instead of doing a lot of casting, you can use is_numeric() to see if a variable is a number or a numeric string. Yeah, but the OP wanted to be able to tell an integer from a real number from a string. is_numeric() is going to validate 5, 5.5, and 5.05E6, for example. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] Validating form field text input to be a specificvariable type
Hello John, Wednesday, April 7, 2004, 2:05:22 PM, you wrote: JWH Yeah, but the OP wanted to be able to tell an integer from a real number JWH from a string. is_numeric() is going to validate 5, 5.5, and 5.05E6, for JWH example. What about is_integer() ? -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Validating form field text input to be a specificvariable type
Hi, El mi? 07-04-2004 a las 08:38, Richard Davey escribió: Hello John, Wednesday, April 7, 2004, 2:05:22 PM, you wrote: JWH Yeah, but the OP wanted to be able to tell an integer from a real number JWH from a string. is_numeric() is going to validate 5, 5.5, and 5.05E6, for JWH example. What about is_integer() ? No way, it checks for data types too and it won't match because the data in the $_REQUEST is always a string. is_integer(5) will always be false. is_integer(5) will return true -William -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
El mi? 07-04-2004 a las 08:05, John W. Holmes escribió: From: William Lovaton [EMAIL PROTECTED] Instead of doing a lot of casting, you can use is_numeric() to see if a variable is a number or a numeric string. Yeah, but the OP wanted to be able to tell an integer from a real number from a string. is_numeric() is going to validate 5, 5.5, and 5.05E6, for example. well, an integer is a float too, the real problem is when you type 5.5 in a variable that is expected to be integer. In this case is_numeric() won't do the trick. May be using regexps ([0-9]) will be enough to check integer values but I think it is a bit slower. It is always better to use native functions instead of regexps where possible. -William -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
An RegExp will work but is not as fast as native functions, but you could use something like: $len1 = strlen ( $input ); $len2 = strspn ( $input , 1234567890,. ); if ( $len1 == $len2 ) { echo 'yuuhooo'; } else { echo 'd\'oh'; } [...] From: William Lovaton [EMAIL PROTECTED] Instead of doing a lot of casting, you can use is_numeric() to see if a variable is a number or a numeric string. Yeah, but the OP wanted to be able to tell an integer from a real number from a string. is_numeric() is going to validate 5, 5.5, and 5.05E6, for example. well, an integer is a float too, the real problem is when you type 5.5 in a variable that is expected to be integer. In this case is_numeric() won't do the trick. May be using regexps ([0-9]) will be enough to check integer values but I think it is a bit slower. It is always better to use native functions instead of regexps where possible. [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
I guess that works but every possible solution posted in this thread is a lot of lines of code to perform a simple validation. PHP should offer/modify the appropriate API to do this. I remember that is_numeric() didn't exist before. Something like: is_float_string(mixed value) or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) is_numeric(5.5, true) == false, 5.5 is not an integer string is_numeric(10, true) == true May be filing a bug report will do the trick. ;-) -William El mi? 07-04-2004 a las 10:25, Red Wingate escribió: An RegExp will work but is not as fast as native functions, but you could use something like: $len1 = strlen ( $input ); $len2 = strspn ( $input , 1234567890,. ); if ( $len1 == $len2 ) { echo 'yuuhooo'; } else { echo 'd\'oh'; } [...] From: William Lovaton [EMAIL PROTECTED] Instead of doing a lot of casting, you can use is_numeric() to see if a variable is a number or a numeric string. Yeah, but the OP wanted to be able to tell an integer from a real number from a string. is_numeric() is going to validate 5, 5.5, and 5.05E6, for example. well, an integer is a float too, the real problem is when you type 5.5 in a variable that is expected to be integer. In this case is_numeric() won't do the trick. May be using regexps ([0-9]) will be enough to check integer values but I think it is a bit slower. It is always better to use native functions instead of regexps where possible. [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
* Thus wrote William Lovaton ([EMAIL PROTECTED]): or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) um.. ctype_digit(). Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
Hi Curt, El mi? 07-04-2004 a las 10:29, Curt Zirzow escribió: * Thus wrote William Lovaton ([EMAIL PROTECTED]): or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) um.. ctype_digit(). Very nice! it works fine. The only issue is that it will return true if the parameter is an empty string (). Nothing that an empty() can't fix. -William -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
No, as this would be yet another line of source. Maybe get those guys to add another function ctype_digit_and_not_empty(); -- red [...] * Thus wrote William Lovaton ([EMAIL PROTECTED]): or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) um.. ctype_digit(). Very nice! it works fine. The only issue is that it will return true if the parameter is an empty string (). Nothing that an empty() can't fix. [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
I don't think so, Wouldn't be better if ctype_digit() return false on an empty string?? any way an empty string is not a digit. This change will brake backward compatibility so it is not an option. if you look carefully the function that you propose is doing two things at once... at this level this is not a good idea. -William El mi? 07-04-2004 a las 12:39, Red Wingate escribió: No, as this would be yet another line of source. Maybe get those guys to add another function ctype_digit_and_not_empty(); -- red [...] * Thus wrote William Lovaton ([EMAIL PROTECTED]): or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) um.. ctype_digit(). Very nice! it works fine. The only issue is that it will return true if the parameter is an empty string (). Nothing that an empty() can't fix. [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
JFYI : var_dump ( '' == 0 ) eq TRUE William Lovaton wrote: I don't think so, Wouldn't be better if ctype_digit() return false on an empty string?? any way an empty string is not a digit. This change will brake backward compatibility so it is not an option. if you look carefully the function that you propose is doing two things at once... at this level this is not a good idea. -William El mi? 07-04-2004 a las 12:39, Red Wingate escribió: No, as this would be yet another line of source. Maybe get those guys to add another function ctype_digit_and_not_empty(); -- red [...] * Thus wrote William Lovaton ([EMAIL PROTECTED]): or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) um.. ctype_digit(). Very nice! it works fine. The only issue is that it will return true if the parameter is an empty string (). Nothing that an empty() can't fix. [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
Yeah, sometimes this is an annoying problem with PHP. Somehow, , null, 0 and 0 is the same thing. The last two are considered empty() too. -William El mi? 07-04-2004 a las 16:34, Red Wingate escribió: JFYI : var_dump ( '' == 0 ) eq TRUE William Lovaton wrote: I don't think so, Wouldn't be better if ctype_digit() return false on an empty string?? any way an empty string is not a digit. This change will brake backward compatibility so it is not an option. if you look carefully the function that you propose is doing two things at once... at this level this is not a good idea. -William El mi? 07-04-2004 a las 12:39, Red Wingate escribió: No, as this would be yet another line of source. Maybe get those guys to add another function ctype_digit_and_not_empty(); -- red [...] * Thus wrote William Lovaton ([EMAIL PROTECTED]): or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) um.. ctype_digit(). Very nice! it works fine. The only issue is that it will return true if the parameter is an empty string (). Nothing that an empty() can't fix. [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
They are typecasted to match the current contexts needs - often a problem but even - but this is one of PHPs strenghs :-) -- red William Lovaton wrote: Yeah, sometimes this is an annoying problem with PHP. Somehow, , null, 0 and 0 is the same thing. The last two are considered empty() too. -William El mi? 07-04-2004 a las 16:34, Red Wingate escribió: JFYI : var_dump ( '' == 0 ) eq TRUE William Lovaton wrote: I don't think so, Wouldn't be better if ctype_digit() return false on an empty string?? any way an empty string is not a digit. This change will brake backward compatibility so it is not an option. if you look carefully the function that you propose is doing two things at once... at this level this is not a good idea. -William El mi? 07-04-2004 a las 12:39, Red Wingate escribió: No, as this would be yet another line of source. Maybe get those guys to add another function ctype_digit_and_not_empty(); -- red [...] * Thus wrote William Lovaton ([EMAIL PROTECTED]): or modify is_numeric() to explictly perform an integer validation: is_numeric(mixed value [, bool is_integer = false]) um.. ctype_digit(). Very nice! it works fine. The only issue is that it will return true if the parameter is an empty string (). Nothing that an empty() can't fix. [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
--- William Lovaton [EMAIL PROTECTED] wrote: Yeah, sometimes this is an annoying problem with PHP. Somehow, , null, 0 and 0 is the same thing. You can always use === if you don't want it to cast. Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly Coming Fall 2004 HTTP Developer's Handbook - Sams http://httphandbook.org/ PHP Community Site http://phpcommunity.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating form field text input to be a specificvariable type
Red, Chris, Good advice, I rarely have to use it, though. -William El mi? 07-04-2004 a las 16:44, Chris Shiflett escribió: --- William Lovaton [EMAIL PROTECTED] wrote: Yeah, sometimes this is an annoying problem with PHP. Somehow, , null, 0 and 0 is the same thing. You can always use === if you don't want it to cast. Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly Coming Fall 2004 HTTP Developer's Handbook - Sams http://httphandbook.org/ PHP Community Site http://phpcommunity.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php