Laravel 验证:数字和整数之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40434642/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:42:02  来源:igfitidea点击:

Laravel validation : difference between numeric and integer?

laravelvalidation

提问by bazi

in laravel docsthere seems to be an integerand a numericvalidation rule. i was wondering what the difference between the both was?

laravel 文档中似乎有一个整数和一个数字验证规则。我想知道两者之间的区别是什么?

回答by Alexey Mezenin

Integer is like a whole number without fraction: 2, 256, 2048.

Integer 就像一个没有分数的整数:2, 256, 2048

http://php.net/manual/en/function.is-int.php

http://php.net/manual/en/function.is-int.php

Numeric is any number including floating point numbers: 2.478, +0123.45e6

数字是任何数字,包括浮点数: 2.478, +0123.45e6

http://php.net/manual/en/function.is-numeric.php

http://php.net/manual/en/function.is-numeric.php

回答by Saumya Rastogi

According to the Laravel's Source Code, both the validations have the following logic.

根据 Laravel 的源代码,这两个验证都有以下逻辑。

// For rule 'integer'
protected function validateInteger($attribute, $value)
{
    return filter_var($value, FILTER_VALIDATE_INT) !== false;
}

// For rule 'numeric'
protected function validateNumeric($attribute, $value)
{
    return is_numeric($value);
}

For more reference dig into the source code of Laravel - here >>

如需更多参考,请深入研究 Laravel 的源代码 -此处>>