Re: variable type - conversion/checking
| From: | Matijn Woudt | Date: | Sat, 16 Mar 2013 02:31:59 +0000 |
| Subject: | Re: variable type - conversion/checking | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320603@lists.php.net to get a copy of this message | ||
On Sat, Mar 16, 2013 at 2:54 AM, Sebastian Krebs <krebs.seb@gmail.com>wrote:
> 2013/3/16 Andrew Ballard <aballard@gmail.com>
>
> > I suppose one could try something like this:
> >
> > if (is_string($val) && $val === (string)(int)$val) ....
> >
> > If $val is an integer masquerading as a string, it should be identical to
> > the original string when cast back to a string, shouldn't it? (I can't
> try
> > it right now.)
> >
>
> It is semantically equivalent to
>
> $val == (int) $val
>
> and as far as I remember (I didn't read everything completely) this were
> mentioned. I didn't read, whether or not, it was accepted as solution ;)
>
>
Sebastian,
This is not true. $val === (string)(int)$val will do a string compare,
where as $val == (int) $val will do an integer compare, which will convert
the left hand to integer too. Your equation will always evaluate as true.
Andrews' solution should work too, but it depends on what you accept as
integer, in this case you would be able to parse normal numbers, with
optionally a sign and extra 0's in front.
is_numeric will parse those integers, but also hexadecimal, octal and
binary notations, and parses floating point integers. If you don't want the
floating points, you could add an extra check that verifies it is not a
float.
- Matijn