is_int和GET或POST [英] is_int and GET or POST

查看:94
本文介绍了is_int和GET或POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在以下情况下is_int总是返回false?

Why does is_int always return false in the following situation?

echo $_GET['id']; //3
if(is_int($_GET['id']))
    echo 'int'; //not executed

推荐答案

为什么is_int总是返回false?

Why does is_int always return false?

因为$_GET["id"]是字符串,即使它恰好包含数字.

Because $_GET["id"] is a string, even if it happens to contain a number.

您的选择:

  • 使用过滤器扩展名. filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT)将返回一个整数类型的变量(如果该变量存在,不是数组,表示一个整数并且该整数在有效范围之内).否则它将返回false.

  • Use the filter extension. filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT) will return an integer typed variable if the variable exists, is not an array, represents an integer and that integer is within the valid bounds. Otherwise it will return false.

强制将其强制转换为整数(int)$_GET["id"]-可能不是您想要的,因为您无法正确处理错误(即"id"不是数字)

Force cast it to integer (int)$_GET["id"] - probably not what you want because you can't properly handle errors (i.e. "id" not being a number)

使用 ctype_digit() 来确保字符串仅由数字组成,因此是一个整数-从技术上讲,它返回的true也会包含超出int范围的非常大的数字,但是我怀疑这将是一个问题. 但是,请注意,此方法无法识别负数.

Use ctype_digit() to make sure the string consists only of numbers, and therefore is an integer - technically, this returns true also with very large numbers that are beyond int's scope, but I doubt this will be a problem. However, note that this method will not recognize negative numbers.

请勿使用:

这篇关于is_int和GET或POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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