php 使用PHP检查变量是否包含正整数的最短方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2559923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shortest way to check if a variable contains positive integer using PHP?
提问by Naveed
I want to check if user input a positive integer number.
我想检查用户是否输入了一个正整数。
1 = true
+10 = true
.1 = false
-1 = false
10.5 = false
Just a positive number.
No characters.
No special character.
No dot.
No minus sign.
I tried is_int()function but it is returning false even on positive integers. Is there a string to intproblem?
我尝试过is_int()函数,但即使在正整数上它也返回 false。是否有字符串到 int 的问题?
回答by Yacoby
Something like this should work. Cast the value to an integer and compare it with its original form (As we use ==rather than ===PHP ignores the type when checking equality). Then as we know it is an integer we test that it is > 0. (Depending on your definition of positive you may want >= 0)
像这样的事情应该有效。将值转换为整数并将其与其原始形式进行比较(因为我们使用==而不是===PHP 在检查相等性时忽略类型)。然后我们知道它是一个整数,我们测试它是否 > 0。(取决于您可能想要的正数定义>= 0)
$num = "20";
if ( (int)$num == $num && (int)$num > 0 )
回答by Gordon
Try the native Filter function*
尝试原生过滤器功能*
filter_var($value, FILTER_VALIDATE_INT, array(
'options' => array('min_range' => 1)
));
* if you just want to make sure the input string consists of an arbitrary length digit sequence, use a regex with [0-9] or [\d+]
* 如果您只想确保输入字符串由任意长度的数字序列组成,请使用带有 [0-9] 或 [\d+] 的正则表达式
Examples with filter_var:
示例filter_var:
var_dump( filter_var(1, FILTER_VALIDATE_INT) ); // int(1)
var_dump( filter_var('1', FILTER_VALIDATE_INT) ); // int(1)
var_dump( filter_var('+10', FILTER_VALIDATE_INT) ); // int(10)
var_dump( filter_var(.1, FILTER_VALIDATE_INT) ); // bool(false)
var_dump( filter_var('.1', FILTER_VALIDATE_INT) ); // bool(false)
var_dump( filter_var(-1, FILTER_VALIDATE_INT,
array('options' => array('min_range' => 1))) ); // bool(false)
var_dump( filter_var('-1', FILTER_VALIDATE_INT,
array('options' => array('min_range' => 1))) ); // bool(false)
var_dump( filter_var('2147483648', FILTER_VALIDATE_INT) ); // bool(false)
var_dump( filter_var('0xFF', FILTER_VALIDATE_INT) ); // bool(false)
var_dump( filter_var(0xFF, FILTER_VALIDATE_INT) ); // int(255)
回答by Decko
I would say this is the best way
我会说这是最好的方法
if (is_int($num) && $num > 0)
as typecasting to an int is very slow.
因为类型转换为 int 非常慢。
回答by Imabigfatseacow
I use a regular expression. Very simple if you think about it. You need more punctuation marks if you want to make a number not a whole positive integer (minus sign and a period). So this just makes sure the only thing you have are numbers 0-9 for the value.
我使用正则表达式。如果你考虑一下,这很简单。如果你想让一个数字不是一个完整的正整数(减号和一个句点),你需要更多的标点符号。因此,这只是确保您拥有的唯一值是数字 0-9。
if(!ereg('^[0-9]+$', $value)) {
$errors .= "This is not a positive whole number";
}
You could add another part on there to make sure it is less than a certain amount of characters as well. Hope this helps.
您可以在那里添加另一部分以确保它也少于一定数量的字符。希望这可以帮助。
回答by Nopcea Flavius
the easiest way is:
最简单的方法是:
if intval($x) > 0 echo "true"
如果 intval($x) > 0 回声“真”
回答by hram908
if(!preg_match('/^[0-9]+$/', $input)) {
if(!preg_match('/^[0-9]+$/', $input)) {
Deprecated: Function ereg() is deprecated
已弃用:不推荐使用函数 ereg()

![php 如何摆脱“未捕获的 SoapFault 异常:[客户端] 看起来我们没有 XML 文档......”错误](/res/img/loading.gif)