PHP检查变量是否为整数

spa*_*key 30 php double integer numbers

我有这个PHP代码:

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
Run Code Online (Sandbox Code Playgroud)

我想知道的是,如何检查$ entityElementCount是一个整数(2,6,...)还是部分(2.33,6.2,...).

谢谢!

Tyl*_*ter 45

if (floor($number) == $number)
Run Code Online (Sandbox Code Playgroud)

  • 试试这个,$ number =( - (4.42-5))/ 0.29; 在计算$ number = 2之后,但上述条件未能说明它是一个整数 (4认同)
  • 在$ number上尝试var_dump.这是一个浮动,这就是为什么这不起作用.花车不精确. (2认同)
  • 正确的用法是if(is_numeric($ number)&& floor($ number)== $ number)...因为floor('banana')=='banana'是真的. (2认同)

Jos*_*eph 39

我知道这是旧的,但我想我会分享一些我刚发现的东西:

使用fmod并检查0

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

fmod与%不同,因为如果你有一个分数,%似乎对我不起作用(它返回0 ...例如,echo 9.4 % 1;将输出0).使用fmod,你将获得分数部分.例如:

echo fmod(9.4, 1);

会输出 0.4


gho*_*g74 18

$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";
}
Run Code Online (Sandbox Code Playgroud)

  • 注意:`ctype_digit('10 .00')`将返回FALSE,在此示例中指示"not whole number" (9认同)
  • 对不起,我已经赶紧回答了.您只需要在使用ctype_digit函数之前将$ entityElementCount转换为字符串,以便上面的代码可以工作. (4认同)
  • 所以,if(ctype_digit((string)$ entityElementCount))应该这样做. (3认同)

Mar*_*cka 10

我会像这样使用intval函数:

if($number === intval($number)) {

}
Run Code Online (Sandbox Code Playgroud)

测试:

var_dump(10 === intval(10));     // prints "bool(true)"
var_dump("10" === intval("10")); // prints "bool(false)"
var_dump(10.5 === intval(10.5)); // prints "bool(false)"
var_dump("0x539" === intval("0x539")); // prints "bool(false)"
Run Code Online (Sandbox Code Playgroud)

其他方案

1)

if(floor($number) == $number) {   // Currently most upvoted solution: 
Run Code Online (Sandbox Code Playgroud)

测试:

$number = true;
var_dump(floor($number) == $number); // prints "bool(true)" which is incorrect.
Run Code Online (Sandbox Code Playgroud)

2)

if (is_numeric($number) && floor($number) == $number) {
Run Code Online (Sandbox Code Playgroud)

转角案例:

$number = "0x539";
var_dump(is_numeric($number) && floor($number) == $number); // prints "bool(true)" which depend on context may or may not be what you want
Run Code Online (Sandbox Code Playgroud)

3)

if (ctype_digit($number)) {
Run Code Online (Sandbox Code Playgroud)

测试:

var_dump(ctype_digit("0x539")); // prints "bool(false)"
var_dump(ctype_digit(10)); // prints "bool(false)"
var_dump(ctype_digit(0x53)); // prints "bool(false)"
Run Code Online (Sandbox Code Playgroud)


Ais*_*ina 9

Chacha说的基本方法是

if (floor($number) == $number)
Run Code Online (Sandbox Code Playgroud)

但是,浮点类型无法准确存储数字,这意味着1可能存储为0.999999997.这当然意味着上述检查将失败,因为它将向下舍入为0,即使为了您的目的,它足够接近 1被视为整数.因此尝试这样的事情:

if (abs($number - round($number)) < 0.0001)
Run Code Online (Sandbox Code Playgroud)


Ant*_*ony 6

如果你知道它将是数字(意味着它不会是一个整数强制转换为字符串,比如"ten""100",你可以使用is_int():

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

echo ($entityWholeNumber) ? "Whole Number!" : "Not a whole number!";
Run Code Online (Sandbox Code Playgroud)


Ant*_*dei 5

我测试了所有提出的解决方案,其中提到了许多有问题的值,它们至少在一个测试用例中都失败了。开始检查是否$value是一个数字,使用is_numeric($value)可以减少许多解决方案的失败次数,但不会将任何解决方案转化为最终解决方案:

$test_cases = array(0.29, 2, 6, 2.33, 6.2, '10.00', 1.4, 10, "10", 10.5, "0x539", true,
    false, 0x53, 9.4, "ten", "100", 1, 0.999999997, 0, 0.0001, 1.0, 0.9999999,
    (-(4.42-5))/0.29);

function is_whole_number($value) {
    // Doing this prevents failing for values like true or "ten"
    if (!is_numeric($value)) {
        return false;
    }

    // @ghostdog74's solution fails for "10.00"
    // return (ctype_digit((string) $value));

    // Both @Maurice's solutions fails for "10.00"
    // return ((string) $value === (string) (int) $value);
    // return is_int($value);

    // @j.hull's solution always returns true for numeric values
    // return (abs($value) % 1 == 0 ? true : false);

    // @ MartyIX's solution fails for "10.00"
    // return ($value === intval($value));

    // This one fails for (-(4.42-5))/0.29
    // return (floor($value) == $value);

    // This one fails for 2
    // return ctype_digit($value);

    // I didn't understand Josh Crozier's answer

    // @joseph4tw's solution fails for (-(4.42-5))/0.29
    // return !(fmod($value, 1) != 0);

    // If you are unsure about the double negation, doing this way produces the same
    // results:
    // return (fmod($value, 1) == 0);

    // Doing this way, it always returns false 
    // return (fmod($value, 1) === 0);

    // @Anthony's solution fails for "10.00"
    // return (is_numeric($value) && is_int($value));

    // @Aistina's solution fails for 0.999999997
    // return (abs($value - round($value)) < 0.0001);

    // @Notinlist's solution fails for 0.999999997
    // return (round($value, 3) == round($value));
}

foreach ($test_cases as $test_case) {
    var_dump($test_case);
    echo ' is a whole number? ';
    echo is_whole_number($test_case) ? 'yes' : 'no';
    echo "\n";
}
Run Code Online (Sandbox Code Playgroud)

我认为像@Aistina 和@Notinlist 提出的解决方案是最好的解决方案,因为它们使用错误阈值来确定值是否为整数。重要的是要注意,它们对表达式按预期工作(-(4.42-5))/0.29,而所有其他人在该测试用例中都失败了。

我决定使用@Notinlist 的解决方案,因为它的可读性:

function is_whole_number($value) {
    return (is_numeric($value) && (round($value, 3) == round($value)));
}
Run Code Online (Sandbox Code Playgroud)

我需要测试值是整数、货币还是百分比,我认为 2 位数的精度就足够了,所以 @Notinlist 的解决方案符合我的需要。

运行此测试:

$test_cases = array(0.29, 2, 6, 2.33, 6.2, '10.00', 1.4, 10, "10", 10.5, "0x539", true,
    false, 0x53, 9.4, "ten", "100", 1, 0.999999997, 0, 0.0001, 1.0, 0.9999999,
    (-(4.42-5))/0.29);

function is_whole_number($value) {
    return (is_numeric($value) && (round($value, 3) == round($value)));
}

foreach ($test_cases as $test_case) {
    var_dump($test_case);
    echo ' is a whole number? ';
    echo is_whole_number($test_case) ? 'yes' : 'no';
    echo "\n";
}
Run Code Online (Sandbox Code Playgroud)

产生以下输出:

float(0.29)
 is a whole number? no
int(2)
 is a whole number? yes
int(6)
 is a whole number? yes
float(2.33)
 is a whole number? no
float(6.2)
 is a whole number? no
string(5) "10.00"
 is a whole number? yes
float(1.4)
 is a whole number? no
int(10)
 is a whole number? yes
string(2) "10"
 is a whole number? yes
float(10.5)
 is a whole number? no
string(5) "0x539"
 is a whole number? yes
bool(true)
 is a whole number? no
bool(false)
 is a whole number? no
int(83)
 is a whole number? yes
float(9.4)
 is a whole number? no
string(3) "ten"
 is a whole number? no
string(3) "100"
 is a whole number? yes
int(1)
 is a whole number? yes
float(0.999999997)
 is a whole number? yes
int(0)
 is a whole number? yes
float(0.0001)
 is a whole number? yes
float(1)
 is a whole number? yes
float(0.9999999)
 is a whole number? yes
float(2)
 is a whole number? yes
Run Code Online (Sandbox Code Playgroud)