Home  >  Article  >  php教程  >  PHP判断FORM表单或URL参数来的数据是否为整数的方法,formurl

PHP判断FORM表单或URL参数来的数据是否为整数的方法,formurl

WBOY
WBOYOriginal
2016-06-13 08:43:42828browse

PHP判断FORM表单或URL参数来的数据是否为整数的方法,formurl

PHP判断FORM表单或URL参数来的数据是否为整数,is_int函数对于FORM表单或URL参数过来的数据是没有办法判断是否是整数的,因为FORM过来的是字符串。
用is_numeric可以判断是否为数字类型,再判断是否有小数点就可以判断是不是整数了

if(!is_numeric($page)||strpos($page,".")!==false){
echo "不是整数";
}else{
echo "是整数";
}

有时候我们需要判断id是否为数字的方法:

dedecms中的判断数字的方法

$cid = empty($cid)? 1 : intval(preg_replace("/[^-\d]+[^\d]/",'', $cid));

建议大家对关键的参数必须做过滤。如数字正则过滤
复制代码 代码如下:
if(preg_match("/^\d*$/",$fgid))    echo('是数字');
else   echo('不是数字');

或者用函数
复制代码 代码如下:
if(is_numeric($fgid)) echo('是数字');
else echo('不是数字');

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn