我正在尝试检测输入是用户ID还是用户名。我试过这个
if (is_int($user)) {
echo "is int";
}else{
echo "is not";
}
然而,它无法正常工作,如果是的话,它不会将其检测为int。我考虑过做(int) $user
,但是如果他们有像“example2”这样的用户名,它会变成2,然后成为id 2吗?
答案 0 :(得分:3)
如果您想检查$user
是否为数字,您可以使用is_numeric
。这可以起作用,具体取决于允许的用户名类型。例如:
is_numeric("+0123.45e6") === true
您可以在PHP docs中看到其他一些示例。
如果您确实需要验证$user
是否仅包含数字字符,则可能需要使用ctype_digit
。
if (ctype_digit($user)){
echo "is int";
} else {
echo "is not";
}
您可以查看this question以了解更多可能性。
答案 1 :(得分:2)
您需要使用is_numeric
if(is_numeric($user)){
echo "is int";
}else{
echo "is not";
}
答案 2 :(得分:0)
尝试使用
if(is_numeric($user)){