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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With