检查变量在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
我将使用
查看数据库的不同页面"(结果1-10、11-20等).但是,我似乎无法使is_int()函数正常工作.在网址(noobs.php?p = 1)中输入"1"会给我带来无效的页面错误,以及诸如"asdf"之类的信息.
which I am going to use for looking at different "pages" of a database (results 1-10, 11-20, etc). I can't seem to get the is_int() function to work correctly, however. Putting "1" into the url (noobs.php?p=1) gives me the invalid page error, as well as something like "asdf".
使用 is_numeric()
检查变量是否为整数不是一个好主意.例如,此函数将为3.14
返回TRUE
.这不是预期的行为.
Using is_numeric()
for checking if a variable is an integer is a bad idea. This function will return TRUE
for 3.14
for example. It's not the expected behavior.
要正确执行此操作,可以使用以下选项之一:
To do this correctly, you can use one of these options:
考虑此变量数组:
$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) === false ) {
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 ✔
第二个选项(比较方式):
# 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 ✔
第四个选项(正则表达式):
# 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 ✔