检查变量是否为自然数 [英] Check if a variable is a natural number

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

问题描述

我想在网站上建立一个出价系统.这意味着用户可以发布其出价(自然数).我想确保用户不要尝试发布字符,十进制数字等. 我不想使用is_numeric函数,因为允许使用十六进制表示法. 我当时想为此使用preg_match.但是在php.net中,有关此功能的文档很少,我也不知道如何使用preg_match.

I want to make a bid system on a website. That means users can post their bid (natural number). I want to make sure users don't try to post characters, decimal numbers, etc. I don't want to use is_numeric function because hexadecimal notation is allowed. I was thinking to use preg_match for this. But in php.net the documentation for this function is little and I have no idea how to use preg_match.

那么我应该如何检查变量是否为带有preg_match的自然数?

So how should I check if a variable is a natural number with preg_match?

推荐答案

如果您不需要小数点: filter_var($var, FILTER_VALIDATE_FLOAT) .

If you don't require decimal points: ctype_digit or filter_var($var, FILTER_VALIDATE_INT).
If you do: filter_var($var, FILTER_VALIDATE_FLOAT).

这篇关于检查变量是否为自然数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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