PHP验证整数 [英] php validate integer

查看:95
本文介绍了PHP验证整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么这不起作用

I`m wonder why this not working

    echo gettype($_GET['id']); //returns string
  if(is_int($_GET['id']))
  {
   echo 'Integer';
  }

如果是整数,如何验证从GET/POST传递的数据?

How to validate data passing from GET/POST if it is integer ?

推荐答案

手动 说:

测试变量是数字还是数字 数字字符串(例如表单输入, 始终是一个字符串),您必须 使用is_numeric().

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

或者,您也可以使用基于正则表达式的测试:

Alternative you can use the regex based test as:

if(preg_match('/^\d+$/',$_GET['id'])) {
  // valid input.
} else {
  // invalid input.
}

这篇关于PHP验证整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆