is_int,is_numeric,is_float和HTML表单验证 [英] is_int, is_numeric, is_float, and HTML form validation

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

问题描述

我的HTML表单上的选择字段可能会产生1到5(整数)。因为 $ _ POST ['rating'] 被视为一个字符串,所以使用 is_int 每次都会拒绝它。



在查阅了PHP手册之后,它似乎是 is_numeric()&& !is_float()是验证整数的正确方法。

我将使用 is_numeric()来解决这个问题。

,因为用户输入总是以字符串形式出现(据我所知)。



保证某些东西的另一种方法是将其转换为一个整数......

p>

  $ id =(int)$ id; 


A select field on my HTML form may yield 1 to 5 (integers). Using is_int rejects it every time, because the $_POST['rating'] is viewed as a string.

After consulting the PHP Manual, it seems is_numeric() && !is_float() is the proper way to validate for an integer in this case.

But I want to be certain, so please confirm or fire away at my logic.

解决方案

I would use is_numeric(), because user input always comes in as a string (as far as I know).

Another way to guarantee something is an integer is to cast it...

$id = (int) $id;

这篇关于is_int,is_numeric,is_float和HTML表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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