RE: [PHP] How to find if a string is an integer?
| From: | Ford, Mike [LSS] | Date: | Fri, 13 Jun 2003 09:16:31 +0000 |
| Subject: | RE: [PHP] How to find if a string is an integer? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-151340@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Alex Earl [mailto:slide@duke-of-earl.com]
> Sent: 12 June 2003 21:13
>
> > Jumping in a little late here, but what about is_numeric()?
> > Haven't tried it, but the php manual for is_int says:
> > Note: To test if a variable is a number or a numeric
> string (such as
> > form input, which is always a string), you must use is_numeric().
> >
>
> The problem with is_numeric() is that something like "4e4" comes up as
> true, because it could be construed as a HEX value.
Just a little nitpick here, but 4e4 is not hex but scientific notation
representing 40,000. 0x4e4 would be a hex value representing, ooooh, a
little over 1200 at a rough guess.
I'm pretty sure both of these have been suggested, but personally I'd go for
either a pattern match or something like:
if ((string)($value+0)===$value)
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211