PHP check if variable is a whole number

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

    I know this is a super old post but this is a simple function that will return a valid whole number and cast it to an int. Returns false if it fails.

    function isWholeNumber($v)
    {
        if ($v !='' && is_numeric($v) && strpos($v, '.') === false) {
            return (int)$v;
        }
        return false;
    }
    

    Usage :

    $a = 43;
    $b = 4.3;
    $c = 'four_three';
    
    isWholeNumber($a) // 43
    isWholeNumber($b) // false
    isWholeNumber($c) // false
    

提交回复
热议问题