在PHP中检查变量是否为整数
我有以下代码在PHP中检查变量是否为整数,php,Php,我有以下代码 $page = $_GET['p']; if($page == "") { $page = 1; } if(is_int($page) == false) { setcookie("error", "Invalid page.", time()+3600); header("location:somethingwentwrong.php"); die(); }
$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
我将使用它查看数据库的不同“页面”(结果1-10、11-20等)。但是,我似乎无法使is_int()函数正常工作。将“1”放入url(noobs.php?p=1)会导致无效页面错误,以及类似“asdf”的错误。所有
$\u GET
参数都有字符串数据类型,因此,is_int
将始终返回false
您可以通过以下方式查看:
使用将提供所需的结果(请注意,它允许以下值:
0x24
) 值$\u GET
始终是字符串–这就是GET参数的形式。因此,是int($\u GET[…])
总是false
您可以测试字符串是否仅由数字组成(即可以解释为数字)。当浏览器在查询字符串中发送
p
时,它将作为字符串而不是int接收。is_int()
因此将始终返回false
相反,请尝试使用is\u numeric()
或
尝试强制转换并检查初始值是否为数字。您可以尝试使用强制转换运算符将其转换为整数:
$page = (int) $_GET['p'];
if($page == "")
{
$page = 1;
}
if(empty($page) || !$page)
{
setcookie("error", "Invalid page.", time()+3600);
header("location:somethingwentwrong.php");
die();
}
//else continue with code
我刚才也有类似的问题 您可以将
filter\u input()
函数与filter\u VALIDATE\u INT
和filter\u NULL\u ON\u FAILURE
一起使用,仅从$\u GET
变量中过滤整数值。工作非常准确!:)
在这里查看我的问题:/!\最佳anwser不正确,is_numeric()为整数和所有数字形式(如“9.1”)返回true 仅对于整数,您可以使用不友好的preg_匹配(“/^\d+$/”,$var)或显式且快2倍的比较:
if ((int) $var == $var) {
// $var is an integer
}
PS:我知道这是一篇老文章,但仍然是谷歌搜索“php是整数”的第三篇文章。doctormad的解决方案不正确。试试这个:
$var = '1a';
if ((int) $var == $var) {
var_dump("$var is an integer, really?");
}
这张照片
1a is not int
1.1 is not int
1e is not int
0x24 is not int
9.2233720368548E+18 is not int
1a是一个整数,真的吗?”
与FILTER\u VALIDATE\u INT参数一起使用
这张照片
1a is not int
1.1 is not int
1e is not int
0x24 is not int
9.2233720368548E+18 is not int
用于检查变量是否为整数是个坏主意。例如,对于3.14
,此函数将返回TRUE
。这不是预期的行为
要正确执行此操作,可以使用以下选项之一:
考虑到这个变量数组:
$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
];
第一个选项(过滤器验证方式):
输出:
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 ✔
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 ✔
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 ✔
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 ✔
第二种选择(铸造比较方式):
输出:
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 ✔
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 ✔
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 ✔
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_数字方式):
输出:
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 ✔
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 ✔
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 ✔
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方式):
输出:
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 ✔
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 ✔
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 ✔
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 ✔
使用is\u int($var)
为了好玩,我测试了上面提到的几种方法,还有一种是我多年来一直使用的解决方案,因为我知道我的输入是正数或字符串等价物 我测试了125000次迭代,每次迭代都传递相同的变量类型和值集 方法1:
是int($value)| ctype|u digit($value)
方法2:
(字符串)(int)$value==(字符串)$value
方法3:
strval(intval($value))==strval($value)
方法4:
ctype\u数字(strval($value))
方法5:
过滤变量($value,filter\u VALIDATE\u 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\u var($value,filter\u VALIDATE\u INT)!==FALSE将获胜
用于获取上述输出的代码:
$u=“-10”;
$v=“0”;
$w=0;
$x=“5”;
$y=“5c”;
$z=1.44;
函数为_int1($value){
返回(is_int($value)| ctype_digit($value));
}
函数为_int2($value){
返回((字符串)(int)$value==(字符串)$value);
}
函数为int 3($value){
返回(strval(intval($value))==strval($value));
}
函数为int 4($value){
返回(ctype_数字(标准($value));
}
函数为int 5($value){
返回filter\u var($value,filter\u VALIDATE\u INT)!==FALSE;
}
函数为int 6($value){
返回值(is_int($value)| ctype_digit($value)| |(is_string($value)&&&$value[0]=='-'&&filter_var($value,filter_VALIDATE_int))!==FALSE);
}
$start=microtime(真);
对于($i=0;$i<125000;$i++){
是国际单位1($u);
国际单位1($v);
国际单位1($w);
is_int1($x);
国际单位1($y);
is_int1($z);
}
$stop=微时间(真);
$start2=微时间(真);
对于($j=0;$j<125000;$j++){
是_