如果声明更有效率的话,写下面的文章的最好方法是什么?
if (!preg\_match('/^[0-9]+$/', $min) || !preg\_match('/^[0-9]+$/', $max) || !preg\_match('/^[0-9]+$/', $c1) || !preg\_match('/^[0-9]+$/', $c2) || !preg\_match('/^[0-9]+$/', $c3) || !preg\_match('/^[0-9]+$/', $c4) || !preg\_match('/^[0-9]+$/', $c5) || ) { echo 'Wrong value" exit; };
提前感谢!
顺便说一句,我在验证整数.全部数字!
发布于 2012-03-19 13:13:27
不如:
$tests = array($min, $max, $c1, $c2, $c3, $c4, $c5);
if (count(preg_grep('/^\d+$/', $tests, PREG_GREP_INVERT)) > 0) {
echo 'Wrong value';
exit;
}preg_grep() docs\d序列,而不是字符类.发布于 2012-03-19 13:12:51
不要使用regexp,只需使用is_numeric http://php.net/manual/en/function.is-numeric.php之类的
is_int( $myvar + 0 )发布于 2012-03-19 13:18:36
这将使您对如何以更整洁的方式测试输入变量有一个概念性的概念。
switch(false)
{
case is_int($min):
case is_int($max):
case is_int($c1):
case is_int($c2):
case is_int($c3):
case is_int($c4):
case is_int($c5):
echo "Wrong value";
break;
default:
break;
}https://stackoverflow.com/questions/9770790
复制相似问题