36

有没有更好的方法来做到这一点?

if( $_POST['id'] != (integer)$_POST['id'] )
    echo 'not a integer';

我试过了

if( !is_int($_POST['id']) )

is_int()由于某种原因不起作用。

我的表格看起来像这样

<form method="post">
   <input type="text" name="id">
</form>

我研究过is_int(),似乎如果

is_int('23'); // would return false (not what I want)
is_int(23);   // would return true

我也试过is_numeric()

is_numeric('23'); // return true
is_numeric(23); // return true
is_numeric('23.3'); // also returns true (not what I want)

似乎这样做的唯一方法是: [这是一种不好的方法,不要这样做,请参阅下面的注释]

if( '23' == (integer)'23' ) // return true
if( 23 == (integer)23 ) // return true
if( 23.3 == (integer)23.3 ) // return false
if( '23.3' == (integer)'23.3') // return false

但是是否有执行上述操作的功能?


只是为了澄清,我想要以下结果

23     // return true
'23'   // return true
22.3   // return false
'23.3' // return false

注意:我刚刚发现我之前提出的解决方案将对所有字符串返回 true。(感谢redreggae)

$var = 'hello';
if( $var != (integer)$var )
    echo 'not a integer';

// will return true! So this doesn't work either.

这不是Checking if a variable is an integer in PHP的重复,因为我对整数的要求/定义与那里不同。

4

12 回答 12

55

尝试ctype_digit

if (!ctype_digit($_POST['id'])) {
    // contains non numeric characters
}

注意:它只适用于string类型。所以你必须转换为string你的正常变量:

$var = 42;
$is_digit = ctype_digit((string)$var);

另请注意:它不适用于负整数。如果你需要这个,你将不得不使用正则表达式。例如,我发现了这个

编辑:感谢 LajosVeres,我添加了 D 修饰符。所以123\n无效。

if (preg_match("/^-?[1-9][0-9]*$/D", $_POST['id'])) {
    echo 'String is a positive or negative integer.';
}

更多:使用强制转换的简单测试将不起作用,因为 "php" == 0 是true并且 "0" === 0 是false!请参阅类型比较表

$var = 'php';
var_dump($var != (int)$var); // false

$var = '0';
var_dump($var !== (int)$var); // true
于 2013-10-07T22:22:21.787 回答
16

尝试filter_var功能

filter_var($_POST['id'], FILTER_VALIDATE_INT);

利用:

if(filter_var($_POST['id'], FILTER_VALIDATE_INT)) { 
    //Doing somethings...

}
于 2014-10-17T01:21:04.350 回答
9

在 PHP$_POST中,值总是文本(string类型)。

您可以将变量强制转换为整数类型,如下所示:

$int_id = (int)$_POST['id'];

如果您确定$_POST['id'] 应该是整数,那将起作用。但是,如果您想绝对确保它仅包含从0to 的数字9并且没有其他符号或符号,请使用:

if( ctype_digit( $_POST['id'] ) )
{
  $int_id = (int)$_POST['id'];
}
于 2013-10-07T22:20:51.027 回答
3

用于is_numeric()检查变量是否为整数是一个坏主意。TRUE例如,此功能将发送3.14。这不是预期的行为

要正确执行此操作,您可以使用以下选项之一:

考虑到这个变量数组:

$variables = [
    "TEST 0" => 0,
    "TEST 1" => 42,
    "TEST 2" => 4.2,
    "TEST 3" => .42,
    "TEST 4" => 42.,
    "TEST 5" => "42",
    "TEST 6" => "a42",
    "TEST 7" => "42a",
    "TEST 8" => 0x24,
    "TEST 9" => 1337e0
];

第一个选项(FILTER_VALIDATE_INT 方式):

# Check if your variable is an integer
if( ! filter_var($variable, FILTER_VALIDATE_INT) ){
  echo "Your variable is not an integer";
}

输出 :

TEST 0 : 0 (type:integer) is not an integer ✘
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

第二种选择(铸造比较方式):

# Check if your variable is an integer
if ( strval($variable) != strval(intval($variable)) ) {
  echo "Your variable is not an integer";
}

输出 :

TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

第三个选项(CTYPE_DIGIT方式):

# Check if your variable is an integer
if( ! ctype_digit(strval($variable)) ){
  echo "Your variable is not an integer";
}

输出 :

TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

第四个选项(REGEX方式):

# Check if your variable is an integer
if( ! preg_match('/^\d+$/', $variable) ){
  echo "Your variable is not an integer";
}

输出 :

TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔
于 2016-11-25T22:50:46.860 回答
2

如果你知道它是一个字符串变量(比如 post o get values),你可以使用:

function is_really_integer($var) {
  return $var == (string)(integer)$var;
}
于 2014-08-28T15:40:48.530 回答
2

检查一下: http: //php.net/manual/en/function.ctype-digit.php - 它验证字符串是否仅包含数字,因此请确保不要将 int 传递给该函数,因为它很可能会返回 false ; 但是,来自$_POST的所有值始终是字符串,因此您很安全。它也不会验证负数,例如-18因为-不是数字,但你总是可以这样做ctype_digit(ltrim($number, '-'))

is_int检查在您的情况下为的变量类型string;为了比较不同类型的两个变量,它与一些真正晦涩的事物(integer)$v === $v相同;==你应该总是使用===,除非你想要"0af5gbd" == 0返回 true

另外,请记住,这ctype_digit不会告诉您字符串是否可以实际转换为有效值int,因为最大整数值为PHP_INT_MAX; 如果你的价值大于那,PHP_INT_MAX无论如何你都会得到。

于 2013-10-07T22:21:35.303 回答
2
preg_match('/^\d+$/D',$variable) //edit 
于 2013-10-07T22:25:10.867 回答
1

使用filter_var但要小心它返回布尔值。

如果你有字符串 '0' => 你会得到 false

正确使用方法:

if (filter_var($_POST['id'], FILTER_VALIDATE_INT) !== false) { 
    // Doing somethings...
}
于 2020-01-30T15:08:51.577 回答
1

使用 ctype_digit 接受的答案是正确的,但是您可以使用函数使生活更轻松。这会将变量转换为字符串,因此您不必:

function is_num($x){
    if(!is_string($x)){
        $x=(string)$x;
    }
    if(ctype_digit($x)){
      return true;
    }
    return false;
}

用法:

if (is_num(56)) {
    // its a number
}

if (is_num('56')) {
    // its a number
}

如果您也想接受小数,请使用以下命令:

function is_num($x){
    if(!is_string($x)){
        $x=(string)$x;
    }    
    if (strpos($x,'.')!==false) {      
        if(substr_count($x,'.')>1||strpos($x,'.')<1||strpos($x,'.')>=strlen($x)){
            return false;    
        }
        $x=str_replace('.','',$x);    
    }
    if(ctype_digit($x)){
        return true;
    }  
    return false;
}
于 2016-02-02T20:45:15.097 回答
0

我用:

is_int($val)||ctype_digit($val)

请注意,这仅捕获正整数字符串

于 2016-10-04T12:35:40.323 回答
0

使用以下通用函数检查所有类型:

function is_digit($mixed) {
    if(is_int($mixed)) {
        return true;
    } elseif(is_string($mixed)) {
        return ctype_digit($mixed);
    }

    return false;
}
于 2016-05-16T10:54:50.857 回答
0

is_int完美运行,不需要额外的代码

或者您可以使用is_numeric检查

于 2017-03-22T07:56:29.593 回答