PHP check if variable is a whole number

后端 未结 20 1219
感动是毒
感动是毒 2020-12-29 20:05

I have this PHP code:

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;

What i want to know is, how to check whether $

20条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 20:22

    I always use typecasting to check if variables contain a whole number, handy when you don't know the origin or type of the value.

    if ((string) $var === (string) (int) $var) {
        echo 'whole number';
    } else {
        echo 'whatever it is, it\'s something else';
    }
    

    In your particular case, I would use is_int()

    if (is_int($var) {
        echo 'integer';
    }
    

提交回复
热议问题