Re: [PHP] variable type - conversion/checking

[email protected] (Matijn Woudt)
Newsgroups php.general
Message-ID <CAC_gtuOHEZ1Z_E0cb7quxVBpLvY+Ouwtr0XmjqY8R_T_qrh0bw@mail.gmail.com>
On Sat, Mar 16, 2013 at 6:52 PM, Maciek Sokolewicz <
[email protected]> 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
>>
>
> All responses in this thread have been very nice; but you could also try a
> much simpler 2-step check:
>
> 1. is_numeric
> 2. if true > check if there's a decimal character in the string:
>
> if(is_numeric($str) && false === strpos('.', $str)) {
>    // it's an int for sure
> } else {
>    // might be a number, but it's definitly not an int
>
> }
>

Wrong.  is_numeric will accept 1e1, which is a float, so you would need to
check for e or E too.

- Matijn
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.