检查变量是否是PHP中的整数

aho*_*ota 67 php

我有以下代码

    $page = $_GET['p'];

    if($page == "")
    {
        $page = 1;
    }
    if(is_int($page) == false)
    {
        setcookie("error", "Invalid page.", time()+3600);
        header("location:somethingwentwrong.php");
        die();
    }
    //else continue with code
Run Code Online (Sandbox Code Playgroud)

我将用它来查看数据库的不同"页面"(结果1-10,11-20等).但是,我似乎无法使is_int()函数正常工作.将"1"放入url(noobs.php?p = 1)会给出无效的页面错误,以及类似"asdf"的内容.

Cam*_*CHN 161

使用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
];
Run Code Online (Sandbox Code Playgroud)

第一个选项(FILTER_VALIDATE_INT方式):

# Check if your variable is an integer
if ( filter_var($variable, FILTER_VALIDATE_INT) === false ) {
  echo "Your variable is not an integer";
}
Run Code Online (Sandbox Code Playgroud)

输出:

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 ?
Run Code Online (Sandbox Code Playgroud)

第二种选择(CASTING COMPARISON方式):

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

输出:

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 ?
Run Code Online (Sandbox Code Playgroud)

第三种选择(CTYPE_DIGIT方式):

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

输出:

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 ?
Run Code Online (Sandbox Code Playgroud)

第四种选择(REGEX方式):

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

输出:

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 ?
Run Code Online (Sandbox Code Playgroud)

  • 请改用filter_var($ int,FILTER_VALIDATE_INT)!== false. (13认同)
  • 注意用FILTER_VALIDATE_INT(你的首选方式)0返回false! (8认同)
  • 'CTYPE_DIGIT way' 和 'REGEX way' 对于负整数都失败。“REGEX 方式”可以通过检查是否有前导负号 (`-?`) 来修复,但我不相信可以以简洁的方式使用 `ctype_digit` 来检查负整数。此外,发布的代码无法产生相应的输出。 (5认同)
  • 或者文本应该改为"它是一个迭代器" (3认同)

Tim*_*per 31

所有$_GET参数都具有字符串数据类型,因此is_int始终返回false.

您可以通过以下方式查看var_dump:

var_dump($_GET['p']); // string(2) "54"
Run Code Online (Sandbox Code Playgroud)

使用is_numeric将提供所需的结果(请注意,允许使用以下值的值0x24).

  • 警告:除了int之外,这将返回true,使用下面的其他解决方案之一. (3认同)

Mic*_*ski 24

当浏览器发送p查询字符串时,它以字符串形式接收,而不是int. is_int()因此总会返回虚假.

相反,尝试is_numeric()ctype_digit()


job*_*ot5 9

/!\ Best anwser不正确,is_numeric()为整数AND返回true,所有数字形式如"9.1"

仅对于整数,您可以使用不友好的preg_match('/ ^\d + $ /',$ var)或显式和2倍快的比较:

if ((int) $var == $var) {
    // $var is an integer
}
Run Code Online (Sandbox Code Playgroud)

PS:我知道这是一个老帖子,但仍然是谷歌寻找"PHP是整数"的第三个


Jon*_*ner 8

只是为了踢,我测试了一些提到的方法,加上一个我用作解决方案多年,当我知道我的输入是正数或字符串等效.

我用125,000次迭代测试了这个,每次迭代都传递了同一组变量类型和值.

方法1: is_int($value) || ctype_digit($value)
方法2: (string)(int)$value == (string)$value
方法3: strval(intval($value)) === strval($value)
方法4: ctype_digit(strval($value))
方法5: filter_var($value, FILTER_VALIDATE_INT) !== FALSE
方法6: is_int($value) || ctype_digit($value) || (is_string($value) && $value[0] === '-' && filter_var($value, FILTER_VALIDATE_INT) !== FALSE)

方法1: 0.0552167892456
方法2: 0.126773834229
方法3: 0.143012046814
方法4: 0.0979189872742
方法5: 0.112988948822
方法6: 0.0858821868896

(我甚至没有测试正则表达式,我的意思是,认真地......正则表达式?)

注意事项:
对于负数(负整数或字符串等效),方法4总是返回false,因此这是一种一致检测值是正整数的好方法.
方法1对于负整数返回true,但对于等于负整数的字符串返回false,因此除非您确定输入永远不会包含字符串或整数形式的负数,否则不要使用此方法,如果确实如此,你的过程不会破坏这种行为.

结论
因此,如果您确定您的输入不包含负数,那么它的使用速度几乎快两倍is_int并且ctype_digit验证您有一个整数.当变量是一个字符串并且第一个字符是破折号时,使用方法1回退到方法5是下一个最快的(特别是当大多数输入是实际整数或字符串中的正数时).总而言之,如果你需要坚实的一致性,并且你不知道数据的混合是什么,并且你必须以一致的方式处理否定,那么filter_var($value, FILTER_VALIDATE_INT) !== FALSE胜利.

用于获取上述输出的代码:

$u = "-10";
$v = "0";
$w = 0;
$x = "5";
$y = "5c";
$z = 1.44;

function is_int1($value){
    return (is_int($value) || ctype_digit($value));
}

function is_int2($value) {
    return ((string)(int)$value == (string)$value);
}

function is_int3($value) {
    return (strval(intval($value)) === strval($value));
}

function is_int4($value) {
    return (ctype_digit(strval($value)));
}

function is_int5($value) {
    return filter_var($value, FILTER_VALIDATE_INT) !== FALSE;
}

function is_int6($value){
    return (is_int($value) || ctype_digit($value) || (is_string($value) && $value[0] === '-' && filter_var($value, FILTER_VALIDATE_INT)) !== FALSE);
}

$start = microtime(TRUE);
for ($i=0; $i < 125000; $i++) {
  is_int1($u);
  is_int1($v);
  is_int1($w);
  is_int1($x);
  is_int1($y);
  is_int1($z);
}
$stop = microtime(TRUE);
$start2 = microtime(TRUE);
for ($j=0; $j < 125000; $j++) {
  is_int2($u);
  is_int2($v);
  is_int2($w);
  is_int2($x);
  is_int2($y);
  is_int2($z);
}
$stop2 = microtime(TRUE);
$start3 = microtime(TRUE);
for ($k=0; $k < 125000; $k++) {
  is_int3($u);
  is_int3($v);
  is_int3($w);
  is_int3($x);
  is_int3($y);
  is_int3($z);
}
$stop3 = microtime(TRUE);
$start4 = microtime(TRUE);
for ($l=0; $l < 125000; $l++) {
  is_int4($u);
  is_int4($v);
  is_int4($w);
  is_int4($x);
  is_int4($y);
  is_int4($z);
}
$stop4 = microtime(TRUE); 

$start5 = microtime(TRUE);
for ($m=0; $m < 125000; $m++) {
  is_int5($u);
  is_int5($v);
  is_int5($w);
  is_int5($x);
  is_int5($y);
  is_int5($z);
}
$stop5 = microtime(TRUE); 

$start6 = microtime(TRUE);
for ($n=0; $n < 125000; $n++) {
  is_int6($u);
  is_int6($v);
  is_int6($w);
  is_int6($x);
  is_int6($y);
  is_int6($z);
}
$stop6 = microtime(TRUE); 

$time = $stop - $start;  
$time2 = $stop2 - $start2;  
$time3 = $stop3 - $start3;  
$time4 = $stop4 - $start4;  
$time5 = $stop5 - $start5;  
$time6 = $stop6 - $start6;  
print "**Method 1:** $time <br>";
print "**Method 2:** $time2 <br>";
print "**Method 3:** $time3 <br>";
print "**Method 4:** $time4 <br>";  
print "**Method 5:** $time5 <br>";  
print "**Method 6:** $time6 <br>";  
Run Code Online (Sandbox Code Playgroud)