Re: [PHP] variable type - conversion/checking

[email protected] (Andrew Ballard)
Newsgroups php.general
Message-ID <CAC1b6Rvsyanr=1hUCei135YzQ7Cpjy9qN1hSf0d43EgUVbwjZg@mail.gmail.com>
On Mar 15, 2013 9:54 PM, "Sebastian Krebs" <[email protected]> wrote:
>
> 2013/3/16 Andrew Ballard <[email protected]>
>>
>> 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 ;)
>

Not quite. That method will massage both values to the same type for the
comparison. Whoever shot it down said they both go to int. If that's the
case,

if ($val == (int)$val)

is more like saying

if ((int)$val === (int)$val)

What I suggested converts the string to an int, the resulting int back to a
string, then does a type-specific comparison to the original string value.

Andrew
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.