11

How can I check if one input have integer value?

For example:

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

}
2

6 Answers 6

32

You should try this:

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


}

OR

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


}
Sign up to request clarification or add additional context in comments.

1 Comment

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
9
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

Comments

2

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";
    }
}
?>

Comments

1

Use is_int:

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

if (is_int($number)) {
   dd('number is an integer');
} else {
   dd('number is not an integer');
}

Comments

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.

Comments

-1

You can also use shorthand if:

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

1 Comment

Using the ternary operator like this is redundant because is_int() already only resolves to true or false.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.