PHP check if variable is a whole number PHP check if variable is a whole number php php

PHP check if variable is a whole number


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


$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;if (ctype_digit($entityElementCount) ){    // (ctype_digit((string)$entityElementCount))  // as advised.    print "whole number\n";}else{    print "not whole number\n";}