PHP check if variable is a whole number

后端 未结 20 1146
感动是毒
感动是毒 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:27

    I know this is old, but I thought I'd share something I just found:

    Use fmod and check for 0

    $entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
    if (fmod($entityElementCount,1) !== 0.0) {
        echo 'Not a whole number!';
    } else {
        echo 'A whole number!';
    }
    

    fmod is different from % because if you have a fraction, % doesn't seem to work for me (it returns 0...for example, echo 9.4 % 1; will output 0). With fmod, you'll get the fraction portion. For example:

    echo fmod(9.4, 1);

    Will output 0.4

提交回复
热议问题