问题描述
if($ _POST ['id']!=(integer) $ _POST ['id'])
echo'not a integer';
我已经尝试了
if(!is_int($ _ POST ['id']))
但是 is_int()
不起作用。
我的表单看起来像这样
< form method =post>
< input type =textname =id>
< / form>
我研究过 is_int()
,看来如果
is_int('23'); //会返回false(不是我想要的)
is_int(23); //会返回true
我也试过 is_numeric() code>
is_numeric('23'); //返回true
is_numeric(23); //返回true
is_numeric('23 .3'); //也返回true(不是我想要的)
[这是一个不好的方法,不要这么做,见下面的注释]
if '23'==(integer)'23')//返回true
if(23 ==(integer)23)//返回true
if(23.3 ==(integer)23.3)//返回false
if('23 .3'==(integer)'23.3')//返回false
但是有没有一个函数可以完成上述工作?
为了澄清,我想得到以下结果
p>
23 //返回true
'23'//返回true
22.3 //返回false
'23.3'//返回false
注:我只是想出了我以前提出的解决方案,将所有字符串返回true。 (感谢redreggae)
$ var ='hello';
if($ var!=(integer)$ var)
echo'not a integer';
//将返回true!所以这也不起作用。
这不是,因为我的整数要求/定义与theres不同。
解决方案尝试
if(!ctype_digit($ _ POST ['id'])){
//包含非数字字符
}
注意:它仅适用于 string
类型。所以你必须转换为 string
你的正常变量:
$ var = 42;
$ is_digit = ctype_digit((string)$ var);
另外请注意:它不适用于负整数。如果你需要这个,你必须使用正则表达式。我找到了例子:
编辑:感谢LajosVeres,我添加了D修饰符。因此 123 \\\
无效。
if(preg_match( / ^ - ?[1-9] [0-9] * $ / D,$ _POST ['id'])){
echo'字符串是正整数或负整数。
$ b 更多:简单测试与铸造将无法工作,因为php== 0是 true
和0=== 0是 false
!
请参阅。
$ var ='php';
var_dump($ var!=(int)$ var); // false
$ var ='0';
var_dump($ var!==(int)$ var); // true
Is there a nicer way to do this?
if( $_POST['id'] != (integer)$_POST['id'] )
echo 'not a integer';
I've tried
if( !is_int($_POST['id']) )
But is_int()
doesn't work for some reason.
My form looks like this
<form method="post">
<input type="text" name="id">
</form>
I've researched is_int()
, and it seems that if
is_int('23'); // would return false (not what I want)
is_int(23); // would return true
I've also tried is_numeric()
is_numeric('23'); // return true
is_numeric(23); // return true
is_numeric('23.3'); // also returns true (not what I want)
[this is a bad way, do not do it, see note below]
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
But is there a function to do the above ?
Just to clarify, I want the following results
23 // return true
'23' // return true
22.3 // return false
'23.3' // return false
Note: I just figured out my previous solution that I presented will return true for all strings. (thanks redreggae)
$var = 'hello';
if( $var != (integer)$var )
echo 'not a integer';
// will return true! So this doesn't work either.
This is not a duplicate of Checking if a variable is an integer in PHP, because my requirements/definitions of integer is different than theres.
解决方案 try ctype_digit
if (!ctype_digit($_POST['id'])) {
// contains non numeric characters
}
Note: It will only work with string
types. So you have to cast to string
your normal variables:
$var = 42;
$is_digit = ctype_digit((string)$var);
Also note: It doesn't work with negative integers. If you need this you'll have to go with regex. I found this for example:
EDIT: Thanks to LajosVeres, I've added the D modifier. So 123\n
is not valid.
if (preg_match("/^-?[1-9][0-9]*$/D", $_POST['id'])) {
echo 'String is a positive or negative integer.';
}
More: The simple test with casting will not work since "php" == 0 is true
and "0" === 0 is false
!See types comparisons table for that.
$var = 'php';
var_dump($var != (int)$var); // false
$var = '0';
var_dump($var !== (int)$var); // true
这篇关于PHP检查,看看如果变量是整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!