Re: is_numeric for php3
| From: | Prottoss | Date: | Tue, 28 Aug 2001 23:37:34 +0000 |
| Subject: | Re: is_numeric for php3 | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-64915@lists.php.net to get a copy of this message | ||
You can use sprintf() to determine if a number is numeric or not.
/*
Return TRUE if numeric and FALSE if not.
*/
function is_numeric_php3($var)
{
if( !isset($var) )
return FALSE;
if( $var==0 )
return TRUE;
if( sprintf("%f", $var) )
return TRUE;
else
return FALSE:
}
This code seems to work and runs faster then a regex.
On Tuesday 28 August 2001 18:42, daniel james wrote:
> Ahhh, in that context, with is_integer(), i think
> you're partially right. however, i looked it up, and
> is_numeric() will work fine.
>
> http://www.php.net/manual/en/function.is-numeric.php
>
> btw-- as for the regexps, none for me, thanks ;)
> those things give me the heebie-jeebies.
>
> --- "scott [gts]" <scott@graphictype.com> wrote:
> > i think he's asking for a numeric test, not an
> > integer test.
> >
> > your example looks like it should fail "3.50" (for
> > example),
> > even though "3.50" is a perfectly valid numeric
> > value.
> >
> > personally, i think you should just use regexps. ;-)
> >
> > i tried to write a little is_num() type function,
> > but i kept
> > getting caught on split(); it seems that split()
> > converts
> > everything to string, no matter what. so if you try
> > to split
> > on the decimal point of 45.67 ... you get two
> > strings, "45" and "67"
> > not 45 and 67, so subsequent is_integer() calls fail
> >
> > (since is_integer() checks the *type* of the
> > variable,
> > not the *value* -- very important to keep in mind)
> >
> > here's what i came up with... perhaps it will be of
> > some
> > help to you (even though it doesnt work)
> >
> >
> > function is_num($number) {
> >
> > // $x contains number, $y contains any decimal
> > value
> > list ($x, $y) = split("\.", $number);
> >
> > // if there's no decimal value
> > if ( empty($y) ) {
> > return is_int($x);
> > }
> > else {
> > return ( is_int($x) && is_int($y) );
> > }
> >
> > }
> >
> > > -----Original Message-----
> > > From: daniel james [mailto:cgi_guy1313@yahoo.com]
> > > Subject: Re: [PHP] is_numeric for php3
> > >
> > > do you mean, as in,
> > >
> > > if !is_integer($var) {
> > > print("$var is not an integer");
> > > }
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail:
> > php-general-unsubscribe@lists.php.net
> > For additional commands, e-mail:
> > php-general-help@lists.php.net
> > To contact the list administrators, e-mail:
> > php-list-admin@lists.php.net
>
> __________________________________________________
> Do You Yahoo!?
> Make international calls for as low as $.04/minute with Yahoo! Messenger
> http://phonecard.yahoo.com/