2

I am trying to detect if the input is a users id or a username. I've tried doing this

if (is_int($user)) {
    echo "is int";
}else{
    echo "is not";
}

However it isn't working, it doesn't detect it as an int if it is. I've thought about doing (int) $user but if they have a username like "example2" it will become 2 and then become the id 2 right?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
jndne
  • 29
  • 7

3 Answers3

3

If you want to check whether $user is numeric, you can use is_numeric. This can work depending on what kinds of usernames are allowed. For example:

is_numeric("+0123.45e6") === true

You can see some other examples in the PHP docs.

If you really need to verify whether or not $user contains only numeric characters, you may want to use ctype_digit instead.

if (ctype_digit($user)){
    echo "is int";
} else {
    echo "is not";
}

You can check this question for some more possibilities.

Community
  • 1
  • 1
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
2

You need to use is_numeric

if(is_numeric($user)){
        echo "is int";
    }else{
        echo "is not";
    }
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
0

Try using

if(is_numeric($user)){
chris85
  • 23,846
  • 7
  • 34
  • 51
Lynn Rey
  • 231
  • 3
  • 17
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 22 '16 at 02:46
  • thanks @FrankerZ . Next time I will make sure that it is well commented before I post it. – Lynn Rey Jul 22 '16 at 15:30