How to check if variable is Alphabetic or Numeric in PHP?
How to check if variable is Alphabetic or Numeric in PHP?
Depending on how you define numeric, you'll use one of the following functions :
is_numeric
ctype_digit
With the first one, numeric is defined as (quoting) :
Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part.
While, with the second one, you will (quoting) :
Checks if all of the characters in the provided string, text, are numerical
And, for alphabetic, you'll be interested by :
ctype_alpha
Quoting :
Checks if all of the characters in the provided string, text, are alphabetic. In the standard C locale letters are just
[A-Za-z]
And, as pointed out by @Long Ears in his comment, if you want to check both in a single shot, you'll find the ctype_alnum()
function (quoting) :
Checks if all of the characters in the provided string, text, are alphanumeric.
In any case, you might want to take a look at the full list of Ctype functions.
Use the is_numeric function:
is_numeric("42"); // true
is_numeric(1337); // true
is_numeric("1e4"); // true
is_numeric("not numeric"); // false
is_numeric(Array()); // false
is_numeric(9.1); // true
You can use is_numeric() or ctype_alpha() functions
you quick test the input val by if numeric or alpha`enter code here`
using this php function
if(ctype_alnum($eafrik)){
echo "is alphatnumeric";
}else{
echo "is not alphanumeric"
}
精彩评论