10

How can I check if one input have integer value?

For example:

if ($request->input('public') == ¿int?){

}
Lluís Puig Ferrer
  • 1,128
  • 7
  • 19
  • 49

6 Answers6

29

You should try this:

if (is_int($request->input('public'))){


}

OR

if (is_numeric($request->input('public'))){


}
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • 2
    is_int() does not work in Lumen 6+ for inputs. It always says false, only is_numeric() works, but that does not have the same functionality – dodancs Feb 23 '20 at 20:24
8
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
Osman B
  • 81
  • 1
  • 1
1

Use is_int:

$number = $request->input('public');

if (is_int($number)) {
   dd('number is an integer');
} else {
   dd('number is not an integer');
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Thiwanka
  • 97
  • 1
  • 8
1

Data taken from the input is always a string.

Use Laravel validation to check if the data from the input is integer or number.

0

You can use ctype_digit

<?php
$cadenas = array('1820.20', '10002', 'wsl!12');
foreach ($cadenas as $caso_prueba) {
    if (ctype_digit($caso_prueba)) {
        echo "La cadena $caso_prueba consiste completamente de dígitos.\n";
    } else {
        echo "La cadena $caso_prueba no consiste completamente de dígitos.\n";
    }
}
?>
Felipe Castillo
  • 536
  • 8
  • 25
-1

You can also use shorthand if:

$isInteger = (is_int($request->input('public'))) ? true : false;