Laravel:检查控制器上的数据是否为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45799538/
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 16:31:48 来源:igfitidea点击:
Laravel: Check if data is integer on controller
提问by Lluís Puig Ferrer
How can I check if one input have integer value?
如何检查一个输入是否具有整数值?
For example:
例如:
if ($request->input('public') == ?int?){
}
回答by AddWeb Solution Pvt Ltd
You should try this:
你应该试试这个:
if (is_int($request->input('public'))){
}
OR
或者
if (is_numeric($request->input('public'))){
}
回答by Osman B
filter_var($request->input('public'), FILTER_VALIDATE_INT) !== false
because:
因为:
- is_int() is true for 12 but isn't for "12"
- is_numerric() is true for 12.12312323
- is_int() 对 12 是真的,但不是对“12”
- is_numerric() 对于 12.12312323 为真
回答by Thiwanka
Use is_int
:
使用is_int
:
$number = $request->input('public');
if (is_int($number)) {
dd('number is an integer');
} else {
dd('number is not an integer');
}
回答by Borbely Andrei
Data taken from the input is always a string.
从输入中获取的数据始终是字符串。
Use Laravel validation to check if the data from the input is integer or number.
使用 Laravel 验证检查输入的数据是整数还是数字。
回答by Rafael Martins
You can also use shorthand if:
如果出现以下情况,您也可以使用速记:
$isInteger = (is_int($request->input('public'))) ? true : false;