Re: variable type - conversion/checking
| From: | Matijn Woudt | Date: | Thu, 14 Mar 2013 23:05:00 +0000 |
| Subject: | Re: variable type - conversion/checking | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320555@lists.php.net to get a copy of this message | ||
On Thu, Mar 14, 2013 at 11:44 PM, Jim Lucas <lists@cmsws.com> wrote:
> On 03/14/2013 11:50 AM, Samuel Lopes Grigolato wrote:
>
>> Something like "if (is_numeric($var)&& $var == floor($var))" will do the
>>
>> trick. I don't know if there's a better (more elegant) way.
>>
>>
>> On Thu, Mar 14, 2013 at 3:09 PM, Matijn Woudt<tijnema@gmail.com> wrote:
>>
>> On Thu, Mar 14, 2013 at 7:02 PM, georg<georg.chambert@telia.com**>
>>> wrote:
>>>
>>> Hi,
>>>>
>>>> I have tried to find a way to check if a character string is possible to
>>>> test whether it is convertible to an intger !
>>>>
>>>> any suggestion ?
>>>>
>>>> BR georg
>>>>
>>>
>>>
>>> You could use is_numeric for that, though it also accepts floats.
>>>
>>> - Matijn
>>>
>>>
>>
> for that type of test I have always used this:
>
> if ( $val == (int)$val ) {
>
> http://www.php.net/manual/en/**language.types.integer.php#**
>
> language.types.integer.casting<http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting>
>
>
I hope you're not serious about this...
When comparing a string and an int, PHP will translate the string to int
too, and of course they will always be equal then.
So:
$a = "abc";
if($a == (int)$a) echo "YES";
else echo "NO";
Will always return YES.
- Matijn