PHP check if variable is a whole number

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

    $entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
    
    Method 1-
        By using ctype_digit() function. 
    
        if ( ctype_digit($entityElementCount )) { 
            echo "Whole Number\n"; 
        } else { 
            echo "Not a whole Number\n"; 
        } 
    
    
    Method 2-
        By using is_float() function. 
    
        if (is_float($entityElementCount )) { 
            echo "Not a Whole Number\n"; 
        } else { 
            echo "Whole Number\n"; 
        } 
    
    
    Method 3-
        By using is_int() function. 
    
        if (is_int($entityElementCount )) { 
            echo "Whole Number\n"; 
        } else { 
            echo "Not a whole Number\n"; 
        } 
    
    
    Method 5-
        By using fmod() function. 
    
        It needs 2 parameters one dividend and other is divisor
        Here $dividend=$entityElementCount and divisor=1
        if (fmod($dividend,$divisor) !== 0.0) {
            echo 'Not a whole number!';
        } else {
         echo 'A whole number!';
        }
    
    there are some more function like intval(), floor(),... can be used to check it`enter code here`
    

提交回复
热议问题