检查正整数(PHP)的最佳方法是什么?

gee*_*guy 42 php validation

我需要检查一个表单输入值是一个正整数(不只是一个整数),我注意到另一个代码片段使用下面的代码:

$i = $user_input_value;
if (!is_numeric($i) || $i < 1 || $i != round($i)) {
  return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

我想知道使用上面的三个检查是否有任何好处,而不仅仅是这样做:

$i = $user_input_value;
if (!is_int($i) && $i < 1) {
  return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

Jef*_*jak 35

不知道为什么没有建议在此使用filter_var.我知道这是一个老线程,但也许它会帮助一个人(毕竟,我最终来到这里,对吧?).

$filter_options = array( 
    'options' => array( 'min_range' => 0) 
);


if( filter_var( $i, FILTER_VALIDATE_INT, $filter_options ) !== FALSE) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

您还可以添加最大值.

$filter_options = array(
    'options' => array( 'min_range' => 0,
                        'max_range' => 100 )
);
Run Code Online (Sandbox Code Playgroud)

http://php.net/manual/en/function.filter-var.php

http://www.php.net/manual/en/filter.filters.validate.php


Sör*_*ren 34

is_numeric($i)如果$ i是一个数字字符串,那么你的两个代码片段之间的差异也会返回true ,但是is_int($i)如果$ i是一个整数,则只返回true,而如果$ i是整数字符串则不返回true .这就是为什么你应该使用第一个代码片段,如果你还想返回true,如果$ i是一个整数字符串(例如,如果$ i =="19"而不是$ i == 19).

有关更多信息,请参阅这些参考

php is_numeric函数

php is_int函数

  • 只是尝试该代码,我发现即使这还不够,因为它将"123"验证为整数.我需要添加`$ i!== trim($ i)`以捕获前导空格.看起来'123'=='123'即使你确定这两个值都是字符串,但PHP在比较它们时不会将它们视为字符串. (2认同)

Chr*_*n P 17

当变量可以是INTEGER或STRING表示整数时,检查正整数的最佳方法:

 if ((is_int($value) || ctype_digit($value)) && (int)$value > 0 ) { // int }
Run Code Online (Sandbox Code Playgroud)

is_int()如果值类型为,则返回true integer.ctype_digit()如果类型是string但字符串的值是整数,则返回true .

此检查与之间的区别is_numeric()是,is_numeric()即使表示非整数的数值(例如"+0.123"),也会返回true.

  • 到目前为止+1似乎是最干净的 - 它也适用于由数字组成的超长字符串.(即使这个字符串以`-`开头) (2认同)

小智 12

它肯定会走向微优化的领域,但是嘿:我正在研究的代码每天都在咀嚼数百万件物品,而且是周五.所以我做了一些实验......

for ($i = 0; $i < 1000000; $i++) {
    // Option 1: simple casting/equivalence testing
    if ((int) $value == $value && $value > 0) { ... }

    // Option 2: using is_int() and ctype_digit().  Note that ctype_digit implicitly rejects negative values!
    if ((is_int($value) && $value > 0) || ctype_digit($value)) { ... }

    // Option 3: regular expressions
    if (preg_match('/^\d+$/', $value)) { ... }
}
Run Code Online (Sandbox Code Playgroud)

然后,我对整数和字符串值运行了上述测试

选项1:简单的铸造/等效测试

  • 整数:0.3秒
  • 字符串:0.4秒

选项2:使用is_int()和ctype_digit()

  • 整数:0.9秒
  • 字符串:1.45秒

选项3:正则表达式

  • 整数:1.83秒
  • 字符串:1.60秒

也许不出所料,选项1是迄今为止最快的,因为没有函数调用,只是转换.值得注意的是,与其他方法不同,选项1将string-float-integer值"5.0"视为整数:

$valList = array(5, '5', '5.0', -5, '-5', 'fred');
foreach ($valList as $value) {
    if ((int) $value == $value && $value > 0) {
        print "Yes: " . var_export($value, true) . " is a positive integer\n";
    } else {
        print "No: " . var_export($value, true) . " is not a positive integer\n";
    }
}

Yes: 5 is a positive integer
Yes: '5' is a positive integer
Yes: '5.0' is a positive integer
No: -5 is not a positive integer
No: '-5' is not a positive integer
No: 'fred' is not a positive integer
Run Code Online (Sandbox Code Playgroud)

对于您的特定用例而言,这是否是一件好事还是留给读者的练习......


Har*_*sha 5

检查整数的另一种最佳方法是使用正则表达式.您可以使用以下代码检查Integer值.浮点值将为false.

if(preg_match('/^\d+$/',$i)) {
  // valid input.
} else {
  // invalid input.
}
Run Code Online (Sandbox Code Playgroud)

如果你能检查$ i> 0是否更好.