if it has to be an integer and integer only this is how I like to do it.
if (is_numeric($r_haslo) == false || ((int)$r_haslo != $r_haslo)) {
//is an integer
$wszystko_ok = false;
$_SESSION['e_haslo'] = "<i class=\"fas fa-user-times\"></i> Podaj tylko cyfry!";
}
With the updated code above, this now makes sure that $r_haslo
is not a number first, and if it is a number, that the number is not an int. So if it's not a number or not an int then it will save the warning to the session.
This way if I send 23 and "23" both are true.
If I send 23 and "23" to is_int()
function only 23 will be true.
If you never send string input then is_int()
is probably the better way to go.
Update
if (strpos($mystring, ".") || is_numeric($r_haslo) == false || ((int)$r_haslo != $r_haslo))
{
//is an integer
$wszystko_ok = false;
$_SESSION['e_haslo'] = "<i class=\"fas fa-user-times\"></i> Podaj tylko cyfry!";
}
The updated code above will also make sure that there is not a '.' in the input.
Note
if the value is being passed as a number, PHP will automatically convert 12.0 and 12. to 12. if the value is being passed as a string, the srtpos()
method will catch it.